[Tutor] Convert string to bytes

Alan Gauld alan.gauld at btinternet.com
Wed Dec 31 19:53:25 CET 2014

On 31/12/14 18:03, shweta kaushik wrote:
> 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.
It works with a fixed string input but using eval() is a big security risk,
especially if you read the input from a file or network or even a user.
You could potentially find your hard disk being formatted or similar damage.

An explicit conversion using int() is much safer.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list