i have this
str = 'D3'
and i need to trasform in 0xd3 type int and not type string how i can
do this?
if i do
hex(int(str,16) ) i obtain a string and this is not what i need.
thanks Luca
> str = 'D3'
Be careful when choosing names. Here you have clobbered the existing
string type binding to the name ‘str’.
> and i need to trasform in 0xd3 type int and not type string how i can
> do this?
You already have the answer; you used it in your example below. I can
only assume you're wanting something additional; what is that?
> if i do hex(int(str,16) ) i obtain a string and this is not what i
> need.
You either want it as an int, or you want it as a string. Which is it?
>>> foo = 'D3'
>>> int(foo, 16)
211
>>> 0xD3
211
>>> int(foo, 16) == 0xD3
True
--
\ “Human reason is snatching everything to itself, leaving |
`\ nothing for faith.” —Saint Bernard, 1090–1153 |
_o__) |
Ben Finney
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
Luca
On 27 Nov, 10:22, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
>>> # 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
Thanks
Luca
luca72 <luca...@libero.it> writes:
> i'm using pyscard
I don't know what that is; can you give a link to what you're referring
to?
> and for send a command he need a list like this:
>
> cmd = [0xdd,0xff, etc]
Is that Python code? If it is, I've already demonstrated that ‘0xDD’ in
Python code *is* ‘221’. They're different representations that compile
to exactly the same object.
> the problem is that i get a text
> like dd
> and i need to trasform it in 0xdd for the list
Right, which you can do by creating an integer as you've already done:
>>> foo = 'DD'
>>> int(foo, 16)
221
because that *is* the number ‘0xDD’:
>>> int(foo, 16) == 0xDD
True
> and if i use hex i have a sting that is not what i need
So don't do that; use the integer.
--
\ “If you ever reach total enlightenment while you're drinking a |
`\ beer, I bet it makes beer shoot out your nose.” —Jack Handey |
_o__) |
Ben Finney
> i'm using pyscard
>
> and for send a command he need a list like this:
>
> cmd = [0xdd,0xff, etc]
Note that 0xdd is exactly the same as 221:
>>> 0xdd == 221
True
It's just an alternative way to write an integer literal that is sometimes
more convenient. Therefore you don't need the final hex() call; just
s = "D3"
v = int(s, 16)
is enough. To build a cmd list from a list of strings use
>>> string_cmd = ["D3", "FF"]
>>> cmd = [int(s, 16) for s in string_cmd]
>>> cmd
[211, 255]
Again, cmd looks different but is exactly the same as [0xd3, 0xff]:
>>> cmd == [0xd3, 0xff]
True
> i have checked and pyscard accept also the decimal notation,
I'm not sure you ever understood what the problem was, or where, but I'm
happy you feel like you've solved it.
>> i'm using pyscard
>
> I don't know what that is; can you give a link to what you're referring
> to?
Simple story: he has seen the examples with hex literals and didn't know
what they were.
http://www.mbthome.net/ mbt shoes
http://www.nike-airyeezy.com discount nike air yeezy
http://www.mbthome.net/ mbt shoes
+1 QOTW. Great reply.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.