Velocity Reviews > Can string formatting be used to convert an integer to its binaryform ?

# Can string formatting be used to convert an integer to its binaryform ?

fdu.xiaojf@gmail.com
Guest
Posts: n/a

 09-28-2006
Hi,

String formatting can be used to converting an integer to its octal or
>>> a = 199
>>> "%o" % a

'307'
>>> "%x" % a

'c7'

But, can string formatting be used to convert an integer to its binary
form ?

xiaojf

flupke
Guest
Posts: n/a

 09-28-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) schreef:
> Hi,
>
> String formatting can be used to converting an integer to its octal or
>>>> a = 199
>>>> "%o" % a

> '307'
>>>> "%x" % a

> 'c7'
>
> But, can string formatting be used to convert an integer to its binary
> form ?
>
>
>
> xiaojf

I don't actually know how to do it with string formatting but you can
create a simple function to do it.
Here's an example:
http://aspn.activestate.com/ASPN/Coo.../Recipe/219300

Regards,
Benedict

Mirco Wahab
Guest
Posts: n/a

 09-28-2006
Thus spoke (E-Mail Removed) (on 2006-09-28 09:10):

> String formatting can be used to converting an integer to its octal or
> >>> a = 199
> >>> "%o" % a

> '307'
> >>> "%x" % a

> 'c7'
>
> But, can string formatting be used to convert an integer to its binary
> form ?

I didn't fell over this problem so far but
I *would* have expected to find something
like a 'pack' operator (as in Perl).

And voilá, there is (even basically identical to Perl):

from struct import *

a = 199
a_bin_str = pack('L', a)

Regards

Mirco

Gabriel Genellina
Guest
Posts: n/a

 09-28-2006
At Thursday 28/9/2006 05:22, Mirco Wahab wrote:

> > String formatting can be used to converting an integer to its octal or
> > >>> a = 199
> > >>> "%o" % a

> > '307'
> > >>> "%x" % a

> > 'c7'
> >
> > But, can string formatting be used to convert an integer to its binary
> > form ?

>
> a = 199
> a_bin_str = pack('L', a)

Notice that the OP was looking for another thing, given the examples.
Perhaps a better wording would have been "how to convert an integer
to its base-2 string representation".

Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Mirco Wahab
Guest
Posts: n/a

 09-28-2006
Thus spoke Gabriel Genellina (on 2006-09-28 11:05):

> At Thursday 28/9/2006 05:22, Mirco Wahab wrote:
>> > But, can string formatting be used to convert an integer to its binary
>> > form ?

>>
>> a = 199
>> a_bin_str = pack('L', a)

>
> Notice that the OP was looking for another thing, given the examples.
> Perhaps a better wording would have been "how to convert an integer
> to its base-2 string representation".

Yes, you are right. The OP looks for a
'binary (bit) representation ..."
I admit I didn't find out how to format
a value into a bit string in Python.

In Perl, this would be a no-brainer:

\$var = 199;
\$str = sprintf "%0*b", 32, \$var;

and str would contain 00000000000000000000000011000111
on a intel machine.

But where is the %b in Python?

Regards & Thanks

Mirco

bearophileHUGS@lycos.com
Guest
Posts: n/a

 09-28-2006
Mirco Wahab:
> But where is the %b in Python?

Python doesn't have that. You can convert the number to a hex, and then
map the hex digitds to binary strings using a dictionary, like this:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440528

Bye,
bearophile

Frederic Rentsch
Guest
Posts: n/a

 09-28-2006
(E-Mail Removed) wrote:
> Mirco Wahab:
>
>> But where is the %b in Python?
>>

>
> Python doesn't have that. You can convert the number to a hex, and then
> map the hex digitds to binary strings using a dictionary, like this:
> http://aspn.activestate.com/ASPN/Coo.../Recipe/440528
>
> Bye,
> bearophile
>
>

Good idea, but shorter with ->
http://cheeseshop.python.org/pypi/SE/2.2%20beta

>>> import SE
>>> Int_To_Binary = SE.SE (SE.SE ('0=0000 1=0001 2=0010 3=0011 4=0100

5=0101 6=0110 7=0111 8=1000 9=1001 A=1010 a=1010 B=1011 b=1011 C=1100
c=1100 D=1101 d=1101 E=1110 e=1110 F=1111 f=1111')
>>> Int_To_Binary ('%x' % 1234567890')

'01001001100101100000001011010010'

>>> Int_To_Binary.save ('se_definition_files/int_to_binary.se')

>>> SE.SE ('se_definition_files/int_to_binary.se') ('%X' % 987654321)

'00111010110111100110100010110001'

Frederic

Mirco Wahab
Guest
Posts: n/a

 09-28-2006
Thus spoke Frederic Rentsch (on 2006-09-28 20:43):
> (E-Mail Removed) wrote:
>> Mirco Wahab:
>>
>>> But where is the %b in Python?

>>
>> Python doesn't have that. ...
>> http://aspn.activestate.com/ASPN/Coo.../Recipe/440528

>
> Good idea, but shorter with ->
> http://cheeseshop.python.org/pypi/SE/2.2%20beta
> SE.SE ('se_definition_files/int_to_binary.se') ('%X' % 987654321)
> '00111010110111100110100010110001'

I don't really understand here:

- why doesn't have Python such a simple and useful thing as to_binstr(...)
even C++ has one built in,
#include <iostream>
#include <bitset>

int somefunc()
{
int val = 199;
std::cout << std::bitset<32>( val );
...

- why would you favor such complicated solutions
for this (as posted), when you can have this
in one line, e.g.:

def int2bin(num, width=32):
return ''.join(['%c'%(ord('0')+bool((1<<k)&num)) for k in range((width-1),-1,-1)])

(including a string with specifier,this is what I came up with after
looking up some Python docs - maybe you can straighten this a bit ...)

-- but my goggles might be biased,
I don't really emphasize the "Python way"

Regards and thanks

Mirco

Fredrik Lundh
Guest
Posts: n/a

 09-28-2006
Mirco Wahab wrote:

> - why doesn't have Python such a simple and useful thing as to_binstr(...)

useful? really? for what?

</F>

Marc 'BlackJack' Rintsch
Guest
Posts: n/a

 09-28-2006
In <efhdt1\$9a1\$(E-Mail Removed)-halle.de>, Mirco Wahab wrote:

> Thus spoke Frederic Rentsch (on 2006-09-28 20:43):
>> (E-Mail Removed) wrote:
>>> Mirco Wahab:
>>>
>>>> But where is the %b in Python?
>>>
>>> Python doesn't have that. ...
>>> http://aspn.activestate.com/ASPN/Coo.../Recipe/440528

>>
>> Good idea, but shorter with ->
>> http://cheeseshop.python.org/pypi/SE/2.2%20beta
>> SE.SE ('se_definition_files/int_to_binary.se') ('%X' % 987654321)
>> '00111010110111100110100010110001'

>
> I don't really understand here:
>
> - why doesn't have Python such a simple and useful thing as to_binstr(...)

Maybe simple, but useful? And if you really need this it's simple to
implement or look up in the cook book.

> - why would you favor such complicated solutions
> for this (as posted), when you can have this
> in one line, e.g.:
>
> def int2bin(num, width=32):
> return ''.join(['%c'%(ord('0')+bool((1<<k)&num)) for k in range((width-1),-1,-1)])

Yeah, I wonder why not everybody sees the beauty in this cool and
straightforward one liner.

Ciao,
Marc 'BlackJack' Rintsch

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post thunk Ruby 14 04-03-2010 10:08 AM thunk Ruby 0 04-01-2010 10:25 PM thunk Ruby 1 03-30-2010 11:10 AM Randy Kramer Ruby 12 10-25-2007 09:56 PM Polaris431 C Programming 8 12-04-2006 07:00 AM