Just use a bytearray() which is a list of bytes.
Python2:
s = "ABCD"
b = bytearray()
b.extend(s)
Python3:
s = "ABCD"
b = bytearray()
b.extend(map(ord, s))
By the way, don't use str as a variable name since that is builtin.
Hello everyone!
I am trying to convert a string into a byte array. If I for example use the string 'Hello' I want to get something like this to store it in a byte array variable:
b'\x48\x45\x4C\x4C\x4F'
What I tried is following:
buf = bytearray(50) # length of text string varies, so I reserved a buffer for a max length of 50 chars
char_arr = [char for char in text]
for i in range(0, len(char_arr)):
buf[i] = hex(ord(char_arr[i]))
This could possibly be total nonsense and I feel like I am making things way too complicated. The variable "text" is the string i read from. I tried converting the string into a char array to read it into the byte array. While doing that I used hex(ord()) to convert the char into its hexadecimal ASCII representation. I just started programming in this language to use CircuitPython on my Raspberry Pi Pico. I get an error called "can't convert str to int". Any suggestions? Thanks in advance!
Just use a bytearray() which is a list of bytes.
Python2:
s = "ABCD"
b = bytearray()
b.extend(s)
Python3:
s = "ABCD"
b = bytearray()
b.extend(map(ord, s))
By the way, don't use str as a variable name since that is builtin.
encode function can help you here, encode returns an encoded version of the string
In [44]: str = "ABCD"
In [45]: [elem.encode("hex") for elem in str]
Out[45]: ['41', '42', '43', '44']
or you can use array module
In [49]: import array
In [50]: print array.array('B', "ABCD")
array('B', [65, 66, 67, 68])
Convert bytes to ascii and back save in Python? - Stack Overflow
Python 3: How to convert a bytearray to an ASCII string - Stack Overflow
Convert string to ASCII value python - Stack Overflow
Converting String Into Byte Array In ASCII Presentation
Videos
bytes.fromhex(s[4*2:8*2].decode("ascii")).decode("ascii")
//'NR09'
Btw, this would be much easier if you didn't use the conversion from Python : convert a hex string
In that question you have:
b'\x0f\x00\x00\x00NR09G05164\x00'
So you can do
c = b'\x0f\x00\x00\x00NR09G05164\x00'
c[4:8].decode("ascii")
//'NR09'
rc@xxxxx:~$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s=b'0f0000004e52303947303531363400'
>>> s.decode("hex")[4:8]
'NR09'
x = x.decode().encode('ascii',errors='ignore')
You use the encode and decode methods for this, and supply the desired encoding to them. It's not clear to me if you know the encoding beforehand. If you don't know it you're in trouble. You may have to guess the encoding in some way, risking garbage output.
Wrong encoding.
3>> bytearray(b'S\x00t\x00a\x00n\x00d\x00a\x00r\x00d\x00F\x00i\x00r\x00m\x00a\x00t\x00a\x00.\x00i\x00n\x00o\x00').decode('utf-16le')
'StandardFirmata.ino'
But that's not ASCII.
The issue was that I was not specifying a decoding. All I had to do was change decode to decode('utf-16-le')
You can use a list comprehension:
>>> s = 'hi'
>>> [ord(c) for c in s]
[104, 105]
Here is a pretty concise way to perform the concatenation:
>>> s = "hello world"
>>> ''.join(str(ord(c)) for c in s)
'10410110810811132119111114108100'
And a sort of fun alternative:
>>> '%d'*len(s) % tuple(map(ord, s))
'10410110810811132119111114108100'