hex int and string

L

luca72

hello i have a problem

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
 
L

luca72

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

Luca
 
8

84715175

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

Luca

Be careful when choosing names. Here you have clobbered the existing
string type binding to the name ‘str’.
You already have the answer; you used it in your example below. I can
only assume you're wanting something additional; what is that?
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- éšè—被引用文字 -

- 显示引用的文字 -
<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"
target="sport jersey">sport jersey</a></strong>
<a title="sport jersey"
href="http://www.sport-jersey.net" target="sport
jersey"><strong>sports jersey </strong></a><strong> </strong><a
title="ugg women" href="http://www.uggwomen.net" target="ugg
women"><strong>uggboot </strong></a><strong> </strong><a title="nike
market " href="http://www.nike-market.com" target="nike
market"><strong>nike</strong></a></p>
 
L

Lie Ryan

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
 
L

luca72

i have checked and pyscard accept also the decimal notation,

Thanks

Luca

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
 
P

Peter Otten

luca72 said:
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:
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]:
True

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

Be careful when choosing names. Here you have clobbered the existing
string type binding to the name ‘str’.


You already have the answer; you used it in your example below. I can
only assume you're wanting something additional; what is that?


You either want it as an int, or you want it as a string. Which is it?

True

--
\ “Human reason is snatching everything to itself, leaving |
`\ nothing for faith.†—Saint Bernard, 1090–1153 |
_o__) |
Ben Finney
 
M

Marco Mariani

luca72 said:
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.
 
M

Marco Mariani

Ben said:
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.
 
8

84715175

hello i have a problem

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

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"
target="sport jersey">sport jersey</a></strong>
<a title="sport jersey"
href="http://www.sport-jersey.net" target="sport
jersey"><strong>sports jersey </strong></a><strong> </strong><a
title="ugg women" href="http://www.uggwomen.net" target="ugg
women"><strong>uggboot </strong></a><strong> </strong><a title="nike
market " href="http://www.nike-market.com" target="nike
market"><strong>nike</strong></a></p>
 
8

84715175

Simple story: he has seen the examples with hex literals and didn't know
what they were.

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"
target="sport jersey">sport jersey</a></strong>
<a title="sport jersey"
href="http://www.sport-jersey.net" target="sport
jersey"><strong>sports jersey </strong></a><strong> </strong><a
title="ugg women" href="http://www.uggwomen.net" target="ugg
women"><strong>uggboot </strong></a><strong> </strong><a title="nike
market " href="http://www.nike-market.com" target="nike
market"><strong>nike</strong></a></p>
 
J

jessica

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
- 显示引用的文字 -

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"
target="sport jersey">sport   jersey</a></strong>
<a title="sport jersey"
href="http://www.sport-jersey.net" target="sport
jersey"><strong>sports jersey </strong></a><strong>   </strong><a
title="ugg women" href="http://www.uggwomen.net" target="ugg
women"><strong>uggboot </strong></a><strong>   </strong><a title="nike
market " href="http://www.nike-market.com" target="nike
market"><strong>nike</strong></a></p>- éšè—被引用文字 -

- 显示引用的文字 -

http://www.enjoyebay.com/
 
J

jessica

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"
target="sport jersey">sport jersey</a></strong>
<a title="sport jersey"
href="http://www.sport-jersey.net" target="sport
jersey"><strong>sports jersey </strong></a><strong> </strong><a
title="ugg women" href="http://www.uggwomen.net" target="ugg
women"><strong>uggboot </strong></a><strong> </strong><a title="nike
market " href="http://www.nike-market.com" target="nike
market"><strong>nike</strong></a></p>

http://www.mbthome.net/ mbt shoes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top