[Tutor] Convert string to bytes
coolshwetu at gmail.com
Wed Dec 31 19:03:36 CET 2014
I also did the same thing and its working... but i dint use this. I just
did this and it is similar to what you said.
>>> s = '0xFE, 0x01, 0x01, 0x22, 0xFE, 0x02'
>>> packet = eval(s)
(254, 1, 1, 34, 254, 2)
this is tuple, which my microcontroller is able to recognize and respond
On Wed, Dec 31, 2014 at 11:27 PM, Alan Gauld <alan.gauld at btinternet.com>
> 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
>> = 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]
> Alan G
> Author of the Learn to Program web site
> Follow my photo-blog on Flickr at:
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
More information about the Tutor