Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

hex int and string

22 views
Skip to first unread message

luca72

unread,
Nov 27, 2009, 3:54:30 AM11/27/09
to
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

Ben Finney

unread,
Nov 27, 2009, 4:22:38 AM11/27/09
to
luca72 <luca...@libero.it> writes:

> 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

luca72

unread,
Nov 27, 2009, 4:28:01 AM11/27/09
to
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

On 27 Nov, 10:22, Ben Finney <ben+pyt...@benfinney.id.au> wrote:

8471...@qq.com

unread,
Nov 27, 2009, 4:35:19 AM11/27/09
to
> > 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>

Lie Ryan

unread,
Nov 27, 2009, 4:38:37 AM11/27/09
to
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

luca72

unread,
Nov 27, 2009, 4:47:09 AM11/27/09
to
i have checked and pyscard accept also the decimal notation,

Thanks

Luca

Ben Finney

unread,
Nov 27, 2009, 4:47:59 AM11/27/09
to
(Please don't top-post; it makes the message difficult to follow.
Instead, post inline to the quoted material you're responding to, and
remove material that you're not responding to.)

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

Peter Otten

unread,
Nov 27, 2009, 4:48:33 AM11/27/09
to
luca72 wrote:

> 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

Marco Mariani

unread,
Nov 27, 2009, 5:57:26 AM11/27/09
to
luca72 wrote:

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

Marco Mariani

unread,
Nov 27, 2009, 5:59:38 AM11/27/09
to
Ben Finney wrote:

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

8471...@qq.com

unread,
Nov 27, 2009, 10:41:48 PM11/27/09
to

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"

8471...@qq.com

unread,
Nov 27, 2009, 10:42:29 PM11/27/09
to

<p> <strong><a title="sport jersey" href="http://www.jerseysup.com"

jessica

unread,
Nov 27, 2009, 10:43:26 PM11/27/09
to
On 11月27日, 下午4时54分, luca72 <lucabe...@libero.it> wrote:

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

jessica

unread,
Nov 27, 2009, 10:44:59 PM11/27/09
to
> market"><strong>nike</strong></a></p>- 隐藏被引用文字 -
>
> - 显示引用的文字 -

http://www.enjoyebay.com/

jessica

unread,
Nov 27, 2009, 10:45:53 PM11/27/09
to
On 11月27日, 下午6时59分, Marco Mariani <ma...@sferacarta.com> wrote:

http://www.mbthome.net/ mbt shoes
http://www.nike-airyeezy.com discount nike air yeezy

jessica

unread,
Nov 27, 2009, 10:46:47 PM11/27/09
to

Tim Roberts

unread,
Nov 28, 2009, 3:26:33 PM11/28/09
to
Marco Mariani <ma...@sferacarta.com> wrote:

+1 QOTW. Great reply.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

0 new messages