0
a = ['67', '56', '23', '29', '5e', 'eb', '6f', 'c9', 'dc', 'dd', '24', '1b', '00', 'b7', '6b', '69', 'e0', 'ce', 'c9', '2c', '61', '18', '22', '10', 'cb', 'a6', 'd5', '82', 'b2', '5d', 'ef', '32']

Length of a is 32

is there any way to convert above string into bytes format with the same length. i need answer same like below

a = [b'67', '56', '23', '29', '5e', 'eb', '6f', 'c9', 'dc', 'dd', '24', '1b', '00', 'b7', '6b', '69', 'e0', 'ce', 'c9', '2c', '61', '18', '22', '10', 'cb', 'a6', 'd5', '82', 'b2', '5d', 'ef', '32']
Shabari nath k
  • 920
  • 1
  • 10
  • 23
M.Girish Babu
  • 39
  • 1
  • 8
  • 2
    `a = [s.encode() for s in a]` or `a = list(map(str.encode, a))` should both work. But why would you want `b'67'` instead of `b'\x67'` as your result? – kaya3 Mar 29 '21 at 03:01
  • Thanks @kaya3, It's working, may be my requirement was wrong, Thank you very much for the help – M.Girish Babu Mar 29 '21 at 08:14
  • @kaya3, maybe you should post your comments as an answer so it gets accepted. Otherwise, this will stay open :) – Joe Ferndz Mar 29 '21 at 23:15
  • Does this answer your question? [Best way to convert string to bytes in Python 3?](https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – Tomerikoo Mar 29 '21 at 23:18
  • 1
    @JoeFerndz I was mainly asking the OP to confirm that this was actually what they wanted; I've converted it to an answer now. – kaya3 Mar 29 '21 at 23:44

1 Answers1

0

a = [s.encode() for s in a] or a = list(map(str.encode, a)) will both do the conversion you describe.

That said, your strings are two hexadecimal digits each, so it would make more sense to encode them as one byte each, rather than encoding the actual hexadecimal digits. To do this, you can write a = [bytes.fromhex(s) for s in a] or a = list(map(bytes.fromhex, a)).

If you want the bytes in the list as a single bytes object instead of a list of individual bytes, you can write a = bytes.fromhex(''.join(a)).

kaya3
  • 47,440
  • 4
  • 68
  • 97