[Tutor] Convert string to bytes
Alan Gauld
alan.gauld at btinternet.com
Wed Dec 31 18:57:33 CET 2014
On 31/12/14 10:08, shweta kaushik wrote:
> 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.
I'm pretty sure you don;t need that, you only need the integer values of
the hex strings. You can then write those integers directly to your
controller. Assuming I'm right this should work (Python v2.7):
>>> s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'
>>> [int(h,16) for h in s.split(',')]
[254, 1, 1, 34, 254, 2]
>>>
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list