I have been searching for a while how to convert string in python to array of signed and unsigned bytes and to reverse the bytearray into string again.
For eg:
s = "sample data" # for example any string
bytearray = [8, 28, 61, 26, 124, -4, -27, 87, -99, -13, 94, 115, 23, 85, -5, 123, 52, 93, -127, 75, 79, -100, -75, 126, -51, 45, 91, 46, -114, -66, -18, -26, -123, 34, -110, -60, 39, 100, 109, -95, -8, 29, -20, 13, -22, -116, 86, -27, 97, -56, -115, 28, 68, 8, 50, 63, 105, 77, 68, -86, 63, -8, 59, -59, 91, 48, 2, 82, 65, 118, -107, -88, 49, 65, 5, -27, -16, -61, 8, 47, 76, -110, -46, 71, 80, 70, 108, -115, -101, 29, -32, -34, 100, -101, -108, -42, 76, -56, -45, 39, 25, 59, 45, 17]
# should give array of signed and unsigned bytes
marmeladze
6,5863 gold badges28 silver badges49 bronze badges
1 Answer 1
Python doesn't have bytes per-se, and its "bytes" are always unsigned in the sense that they're int between 0 and 255.
If you want "signed bytes" you need to request such parsing explicitly using the struct or array modules (typecode b).
answered Mar 12, 2020 at 8:27
Masklinn
43.8k4 gold badges58 silver badges78 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
bytearray?