3

So I have a binary number, 001000 i.e. 8.

If I do base64.b64encode(001000) I get an error.

So I do base64.b64encode(b'001000') and I get b'MDAxMDAw'.

But I need to get the base64 character in the index of the original number, 001000 or 8, which in this case would be 'I'. Or if the number was 011100 (28), then the corresponding character on the base64 table with a value of 28 would be: 'd'.

For example: here are some values in the b64 table

0 == A, 1 == B, 2 == C, 3 == D, 4 == E

So what I want to do is: First, convert the binary number to decimal (000011 == 3) . Then take that number and compare it to the base64 table, and you will see that 3 or 000011, is equal to 'D'.

Does anyone know how I could do this?

3
  • 1
    you tried 0b001000 ? Commented Jan 5, 2021 at 21:30
  • if you do base64.b64encode(b'0b001000') you get b'MGIwMDEwMDA=' Commented Jan 5, 2021 at 21:32
  • or if you do base64.b64encode(0b001000) you get an error Commented Jan 5, 2021 at 21:33

1 Answer 1

2

If I understand correctly, the following should do what you want:

base64.b64encode(bytes([0b001000]))

Explanation:

  • 0b001000: the 0b notation returns the integer 8
  • [0b001000]: creates an array of length 1
  • bytes([0b001000]): converts an iterable to bytes
  • b64encode(bytes([0b001000])): converts these bytes to base64 encoding
Sign up to request clarification or add additional context in comments.

7 Comments

that returned b'CA==' for me, but what i was hoping for was 'I'
if you look at a base64 table you will see the value column and character column and since the value of 'I' is 8 when i do 001000 i expected an 'I', thanks anyway
CA== is the base64 encoding of the byte 0001000, right? Where does the 'I' come from?
well, 001000 in decimal is 8 and in the base64 table 'I' in base64 is equal to 8 in decimal right?
sorry if that is confusing
|

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.