jeril
jeril

Reputation: 1253

python 3.8.6 - converting a string to byte like object

I have got this value

{'_id'
   {'_data': 825FABE6C1000000012B022C0100296E5A100422555F0A203C4F84A0150B250434473D46645F696400645FABE6C1786E540BB69619380004
   }
}

I retrieve this value using change['_id']['_data'] which is of type string.

How do I convert it to bytes like object in python?

I tried this

base64.b64encode(change['_id']['_data']).decode()

But still I get the error

TypeError: a bytes-like object is required, not 'str' 

Upvotes: 2

Views: 2280

Answers (3)

Czaporka
Czaporka

Reputation: 2407

Note
As pointed out by @SGT, your data happens to be a valid base 16 string, which makes it a bit unlikely that it has actually been encoded with base 64. It is crucial that you know the actual encoding - decoding with a different codec is going to just yield garbage.
/Note

Decoding base64

In your code you're calling base64.b64encode rather than base64.b64decode but for decoding a base-64 str to bytes you need the latter.

>>> import base64
>>> s = "825FABE6C1000000012B022C0100296E5A100422555F0A203C4F84A0150B250434473D46645F696400645FABE6C1786E540BB69619380004"
>>> base64.b64decode(s)
b'\xf3nE\x00\x11:\x0b]4\xd3M4\xd3]\x81\xd3m\x82\xd3]4\xdb\xde\x84\xe4\rt\xd3\x8d\xb6\xe7\x9eE\xd0\r\xb4\xdc.\x05\xf3\x804\xd7\x9d\x01\xdb\x9d8\xdf\x8e;\xdc>:\xeb\x8eE\xeb\xde\xb8\xd3N\xb8\xe4P\x01\x13\xa0\xb5\xef\xce\x84\xe7\x8d\x01\x07\xafz\xd7\xdd\xfc\xd3M8'

Decoding base16

>>> bytes.fromhex(s)
b'\x82_\xab\xe6\xc1\x00\x00\x00\x01+\x02,\x01\x00)nZ\x10\x04"U_\n <O\x84\xa0\x15\x0b%\x044G=Fd_id\x00d_\xab\xe6\xc1xnT\x0b\xb6\x96\x198\x00\x04'
>>> bytearray.fromhex(s)
bytearray(b'\x82_\xab\xe6\xc1\x00\x00\x00\x01+\x02,\x01\x00)nZ\x10\x04"U_\n <O\x84\xa0\x15\x0b%\x044G=Fd_id\x00d_\xab\xe6\xc1xnT\x0b\xb6\x96\x198\x00\x04')

About differences between bytes and bytearray

Upvotes: 2

Niko Fohr
Niko Fohr

Reputation: 33988

If you want the base64 encoded version of your string, you can the encode method of the string with base64.b64encode() to get the your data as base64 encoded bytes:

In [65]: data = "825FABE6C1000000012B022C0100296E5A100422555F0A203C4F84A0150B250434473D46645F696400645FABE6C1786E540BB6
    ...: 9619380004"
In [66]: base64.b64encode(data.encode('utf-8'))
Out[66]: b'ODI1RkFCRTZDMTAwMDAwMDAxMkIwMjJDMDEwMDI5NkU1QTEwMDQyMjU1NUYwQTIwM0M0Rjg0QTAxNTBCMjUwNDM0NDczRDQ2NjQ1RjY5NjQwMDY0NUZBQkU2QzE3ODZFNTQwQkI2OTYxOTM4MDAwNA=='

then, this can be converted to base64 encoded string with the decode() method:

In [67]: base64.b64encode(data.encode('utf-8')).decode()
Out[67]: 'ODI1RkFCRTZDMTAwMDAwMDAxMkIwMjJDMDEwMDI5NkU1QTEwMDQyMjU1NUYwQTIwM0M0Rjg0QTAxNTBCMjUwNDM0NDczRDQ2NjQ1RjY5NjQwMDY0NUZBQkU2QzE3ODZFNTQwQkI2OTYxOTM4MDAwNA=='

In summary

  • str can be converted to bytes with str.encode()
  • bytes can be converted to base64 encoded bytes with base64.b64encode()
  • base64 encoded bytes can be converted to base64 encoded string by calling the decode() method of that object.

Upvotes: 1

SGT
SGT

Reputation: 11

If your data is a hexstring then can use

byteArray = bytes.fromhex(change['_id']['_data'])

Upvotes: 1

Related Questions