[Tutor] Convert string to bytes

shweta kaushik coolshwetu at gmail.com
Wed Dec 31 19:03:36 CET 2014


Hi Alan,

Thank you..

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
back.

Regards,
Shweta

On Wed, Dec 31, 2014 at 11:27 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> 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
>
>
>
> _______________________________________________
> 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