hex int and string

Lie Ryan lie.1296 at gmail.com
Fri Nov 27 04:38:37 EST 2009


On 11/27/2009 8:28 PM, luca72 wrote:
> i'm using pyscard
>
> and for send a command he need a list like this:
>
> cmd = [0xdd,0xff, etc]
>
> the problem is that i get a text
> like dd
> and i need to trasform it in 0xdd for the list and if i use hex i have
> a sting that is not what i need
>

 >>> # Do you know that when you write
 >>> somelist = [0xdd, 0xff, 0x34]
 >>> # python read it as
 >>> somelist
[221, 255, 52]

All int in python (and in fact most computers) is stored as binary 
digits; when you need a textual representation the binary digits is 
transformed into string (even decimal representation [!]). When python 
evaluates an integer literal expression, it converts them from whatever 
base it is originally in [1] to binary.

[1] determined by the prefix: 0b -> binary, 0x -> hexadecimal, 0o -> 
octal, and unprefixed -> decimal.

thus:
 >>> [0xdd, 0xff, 0x34] == [221, 255, 52]
True




More information about the Python-list mailing list