0

I'm currently working on a conversion script to convert between formats.

When I use binascii to convert a user given ascii string to binary a 'b' appears in the output. At first I thought it has something to do with the bytes data format that is required but if I try to reverse the process something completely different appears.

if encodetooption == 'B':
            encodestring = encodestring.encode()
            encodedstring = bin(int(binascii.hexlify(encodestring), 16))

produces this e.g:

0b1100001011011000110010101111000

can someone explain why that is so and how it can be reversed properly or how it can be done better.

whonnock
  • 45
  • 6
  • 1
    Does this answer your question? [What does the 'b' character do in front of a string literal?](https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal) – kennyvh Apr 16 '21 at 16:18
  • @khuynh: No, that's a different thing. – user2357112 Apr 16 '21 at 16:21
  • 1
    ‘0b’ at the beginning is probably indicating a binary codes value as ‘0x’ is indicating a hexadecimal coded value. – ai2ys Apr 16 '21 at 16:24
  • Yeah I imagined it had something to do with the bases. Is there anything you know about to avoid this or will I have to strip it off ? – whonnock Apr 16 '21 at 16:27
  • 1
    Just strip it using ‘bin(value)[2:]’ – ai2ys Apr 16 '21 at 16:38

0 Answers0