11

Lets say I have some binary value:

0b100

and want to convert it to base64

doing base64.b64decode(0b100) tells me that it is expecting a string, not an int.... now, I don't want to work with strings.

So, could anyone point me in the right direction for converting binary numbers to base64 numbers? thanks!

=D

2
  • 1
    Do you really want to convert to radix-64 numbers, or base-64 encoding? Converting to radix-64 numbers is a trivial mathematical computation (in any language). Commented Mar 14, 2011 at 22:40
  • 1
    Just wondering if python has something super easy to use. Commented Mar 14, 2011 at 22:52

3 Answers 3

7

Depending on how you represent the value 0b100

>>> import struct
>>> val = 0b100
>>> print struct.pack('I', val).encode('base64')
BAAAAA==

This will turn your value into a 4-byte integer in native endianness, and encode that value into base64. You need to specify the width of your data as base64 is really made for representing binary data in printable characters.

You can typically encode data as a string of bytes via struct's functions, and then encode into base64. Ensure you're using the same data layout and endianess when decoding.

Sign up to request clarification or add additional context in comments.

3 Comments

That may not be what the questioner is after. That will convert the binary to decimal, and then base 64 encode the decimal representation. If you just want to convert the number to base 64, it's a bit different.
Hm? I'm base64-encoding the binary representation of it, as he asked. (I initially answered it incorrectly by mis-reading his question, but have since fixed it)
Back from base64? Use encoded_string.decode('base64'), then carve that string using struct's unpack.
2

Binary literals are just normal python integers.

>>> 0b100
<<< 4

>>> 4
<<< 4

Just convert to a string and base64 encode it (giving you a string that is the base64 representation of '4'). There is nothing wrong with this approach, it's lossless and simple.

>>> s = str(0b100).encode('base64')

>>> int(s.decode('base64'))
<<< 4

If you want, you can use bin to convert the int into a binary string:

>>> bin(int(s.decode('base64')))
<<< '0b100'

1 Comment

hmm, this doesn't work anymore with python3: LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs
0

How large is your binary value? If it can fit in a single byte, then you can use chr.

>>> base64.b64encode(chr(255))
'/w=='

Otherwise I think you'll have to get the individual bytes out of it with some exponential math and arrange them in big-endian or little-endian, calling chr multiple times. (Edit: yan's answer with the struct module is easier :)

Also, I should note that the binary integer syntax is just that, syntax. The interpreter sees it as just another integer.

>>> (0b0).__class__
<type 'int'>

1 Comment

well, it could be anywhere from 8 bits to 512 bits. =\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.