[Tutor] Convert string to bytes

WolfRage wolfrage8765 at gmail.com
Wed Dec 31 17:34:35 CET 2014


I wrote a program to help me break out hex strings awhile ago. It was 
written to communicate with a Atmega 168. This is written for Python 3. 
Here is a snippet, see if this helps you.

s4 = "28 40 7A 7C 05 00 00 34"
hex_array = bytearray.fromhex(s4)
print(s4)
print(list(hex_array))
print(hex_array)
for byte in list(hex_array):
     print(hex(byte))
     print(bytes([byte]))


On 12/31/2014 05:08 AM, shweta kaushik wrote:
> Hi all,
>
> I need help on this problem.
>
> I have one message packet in form of string s = '0xFE, 0x01, 0x01, 0x22,
> 0xFE, 0x02'. I have to send this data to MSP430 microcontroller, but it is
> not taking data if it is string. If I am passing this as hardcoded value s1
> = 0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02 then board is responding. I want to
> convert s as s1 using python.
>
> Please help me out to convert string in normal format for microcontroller
> to respond.
>
> Thanks in advance.
>
> Regards,
> Shweta
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list