3

I have a string say FhY= which has been encoded to hex. So when run

>>> b6 = 'FhY='
>>> b6.decode('base64')
'\x16\x16'

This is a hex string that once converted should be 22 22. This result has been proven on the site https://conv.darkbyte.ru/. However, I cannot seem to do a proper conversion from base64 to decimal representation. Some of the challenges I am facing are

  1. Expectation of decimal being an int. I just want base 10
  2. Incorrect values. I have tried the following conversions base64 > base16(Convert a base64 encoded string to binary), base64 > binary > decimal(Convert hex string to int in Python) both of which have failed.

Please assist.

4
  • Is there a reason why you aren't using struct? Commented Aug 10, 2016 at 16:09
  • I have not had a look at it. Let me do so now Commented Aug 10, 2016 at 16:11
  • @IgnacioVazquez-Abrams probably because he needs an example :P struct.unpack('H','\x16\x16') maybe? Commented Aug 10, 2016 at 16:11
  • Thanks @JoranBeasley. seems to work. However is there a solution that does not involve a two step process? One straight from base64 to base10? Commented Aug 10, 2016 at 16:14

1 Answer 1

3

You need to convert each byte from the decoded string to its decimal value. So this should solve it:

b6 = 'FhY='
' '.join([ str(ord(c)) for c in b6.decode('base64') ])

Results in 22 22

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

2 Comments

Thanks, looks good. Is this function reversible? How could I map the resulting base-10 text back to the base-64 string?
@Leonid you can reverse the function by using ''.join(map(lambda x: chr(int(x)), '22 22'.split())).encode('base64')

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.