## jmark13

In python I have a very large string of 0s and 1s. i.e '010100100101110101001...' etc.

I want to convert this to BINARY without first converting it to decimal. Then I want to take the resulting binary number and convert it to it's decimal equivalent. Fastest way possible because it is about 72k bits.

OR, If anyone knows maple... How could I perform the python operation:

>string='010101010101'
stringB=string*5
>>print stringB
>'010101010101010101010101010101010101010101010101010101010101'

in Maple?? Its a new programing language for me, but I know how to do binary conversions in it.

-Mark

## griswolf 304

Can you be a little more specific about what you mean by 'convert to binary'? What is the expected type of the result? Or do you just care that it be in some in-memory form so you can take the next step?

And: is that 74 Kbits the number of represented bits, or the size of the ascii string? (not that a 9.25 Kbit binary number is significantly less trouble than a 74 Kbit one)

Finally: What is the actual goal of this code? Do you actually need the internal representation later, or is the "decimal equivalent" all you really need, in the end?

## pyTony 888

Like this:

``````>>> a='010101010101010101010101010101010101010101010101010101010101'
>>> a=int(a,2)
>>> a
384307168202282325L
>>>``````

## pyTony 888

You wanted to express it also in binary format, so I continue

``````>>> bin(384307168202282325L)
'0b10101010101010101010101010101010101010101010101010101010101'
## from this alternative way
a='0b'+a
>>> eval(a)
384307168202282325L
>>>``````

## utpalendu

#!/usr/bin/env python
def makebinary(number):
temp=long(number)
k=0
for i in xrange(64):
mask= 1 << (64 - (i+1))
if k == 0:
print int("0"),
else:
print int("1"),

a='010101010101010101010101010101010101010101010101010101010101'
a=int(a,2)
print a
#makebinary(4)
makebinary(a)

## pyTony 888

#!/usr/bin/env python

``````def makebinary(number):
temp=long(number)
k=0
for i in xrange(64):
mask= 1 << (64 - (i+1))
if k == 0:
print int("0"),
else:
print int("1"),

a='010101010101010101010101010101010101010101010101010101010101'
a=int(a,2)
print a
#makebinary(4)
makebinary(a)``````

You can not do this with that hand coded version:

``````>>> import random
>>> bindig='01'
>>> a=''.join([random.choice(bindig) for a in range(70*1024)])
>>> b=int(a,2)
>>> c=bin(b)
>>> print c[:100],a[:100]
0b11111111110001100010101111111111010100100001011010010111101101001100100011010001101001000111110010 1111111111000110001010111111111101010010000101101001011110110100110010001101000110100100011111001011
>>> print c[:100]; print a[:100]
0b11111111110001100010101111111111010100100001011010010111101101001100100011010001101001000111110010
1111111111000110001010111111111101010010000101101001011110110100110010001101000110100100011111001011
>>> print c[-100:]; print a[-100:]
1000100001011101101101001000001000101011111001110011001011110111001001100101010001000111011001100100
1000100001011101101101001000001000101011111001110011001011110111001001100101010001000111011001100100
>>> print len(c), len(a)
71682 71680

## jmark13

I'm sorry, I want to convert any large decimal *TO* binary not from binary.

The second question was specific to the language maple, not python. I know how to convert to binary in maple and I know how to extend strings in python... I just want to be able to do both in at least one of the two languages.

## pyTony 888

I gave solution to that:

``````import random
bindigits='01'
a = ''.join([random.choice(bindigits) for a in range(80*1024)])
a = '0b1'+a ## bin string starting with 1

b = int(a,2) ## b as normal int
c = bin(b) ## convert BACK TO bin string repr

## comparision for checking of the process back and forth
print c[:100] ; print a[:100]
print c[-100:]; print a[-100:]
print len(c), len(a)``````

For processing check documentation of module decimal.

## pyTony 888

Can you mark this case solved?

## jmark13

Awesome thanks, tonyjv.