0

I want to convert this string into a byte scheme:

www.google.de

So the output should look like:

\x77\x77\x77\x2e\x67\x6f\x6f\x67\x6c\x65\x2e\x64\x65\x2f\x62\x6d\x70\x2e\x62\x6d\x70

I tried things like:

a = ''.join(hex(ord(x))[2:] for x in data)

which gave me

>>> 7777772e676f6f676c65

I also tried:

b = [hex(ord(i)) for i in data[:]]

which resulted in:

>>> ['0x77', '0x77', '0x77', '0x2e', '0x67', '0x6f', '0x6f', '0x67', '0x6c', '0x65']

But this is a list, and I need a bytesarry, which looks more like the one I mentioned above. Also to give a bigger picture: I want to send a bytestream of data to an ATTINY microcontroller and it cannot handle strings, only a bytestream.

xtlc
  • 1,070
  • 1
  • 15
  • 41
  • possible duplicate of [hexadecimal string to byte array in python](http://stackoverflow.com/questions/5649407/hexadecimal-string-to-byte-array-in-python) – Krystian Sakowski Aug 31 '15 at 20:22
  • It looks to me like you're getting exactly what you want...a byte string created by deocding that hex string. If you decode hex `77`, you get `119`, which happens to be ascii `w` and will display as such (e.g., if you `print b'\x77'` you will get `w`). – larsks Aug 31 '15 at 20:44

0 Answers0