hex int and string

Discussion in 'Python' started by luca72, Nov 27, 2009.

  1. luca72

    luca72 Guest

    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
     
    luca72, Nov 27, 2009
    #1
    1. Advertising

  2. luca72

    luca72 Guest

    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 <> wrote:
    > luca72 <> 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, Nov 27, 2009
    #2
    1. Advertising

  3. luca72

    Guest

    On 11月27æ—¥, 下åˆ5æ—¶28分, 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
    >
    > Luca
    >
    > On 27 Nov, 10:22, Ben Finney <> wrote:
    >
    >
    >
    > > luca72 <> 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- éšè—被引用文字 -

    >
    > - 显示引用的文字 -

    <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>
     
    , Nov 27, 2009
    #3
  4. luca72

    Lie Ryan Guest

    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
     
    Lie Ryan, Nov 27, 2009
    #4
  5. luca72

    luca72 Guest

    i have checked and pyscard accept also the decimal notation,

    Thanks

    Luca

    On 27 Nov, 10:38, Lie Ryan <> wrote:
    > 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, Nov 27, 2009
    #5
  6. luca72

    Peter Otten Guest

    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

    > 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 <> wrote:
    >> luca72 <> 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
     
    Peter Otten, Nov 27, 2009
    #6
  7. 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, Nov 27, 2009
    #7
  8. 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.
     
    Marco Mariani, Nov 27, 2009
    #8
  9. luca72

    Guest

    On 11ÔÂ27ÈÕ, ÏÂÎç4ʱ54·Ö, luca72 <> wrote:
    > 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>
     
    , Nov 28, 2009
    #9
  10. luca72

    Guest

    On 11月27æ—¥, 下åˆ6æ—¶59分, Marco Mariani <> wrote:
    > 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.


    <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>
     
    , Nov 28, 2009
    #10
  11. luca72

    jessica Guest

    On 11ÔÂ27ÈÕ, ÏÂÎç4ʱ54·Ö, luca72 <> wrote:
    > 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


    http://www.mbthome.net/ mbt shoes
     
    jessica, Nov 28, 2009
    #11
  12. luca72

    jessica Guest

    On 11月27æ—¥, 下åˆ5æ—¶35分, "" <> wrote:
    > On 11月27æ—¥, 下åˆ5æ—¶28分, 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

    >
    > > Luca

    >
    > > On 27 Nov, 10:22, Ben Finney <> wrote:

    >
    > > > luca72 <> 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- éšè—被引用文字 -

    >
    > > - 显示引用的文字 -

    >
    > <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/
     
    jessica, Nov 28, 2009
    #12
  13. luca72

    jessica Guest

    On 11月27æ—¥, 下åˆ6æ—¶59分, Marco Mariani <> wrote:
    > 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.


    http://www.mbthome.net/ mbt shoes
    http://www.nike-airyeezy.com discount nike air yeezy
     
    jessica, Nov 28, 2009
    #13
  14. luca72

    jessica Guest

    On 11ÔÂ28ÈÕ, ÉÏÎç11ʱ41·Ö, "" <> wrote:
    > On 11ÔÂ27ÈÕ, ÏÂÎç4ʱ54·Ö, luca72 <> wrote:
    >
    > > 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>


    http://www.mbthome.net/ mbt shoes
     
    jessica, Nov 28, 2009
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,220
    Neredbojias
    Aug 19, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,647
    Old Wolf
    Jan 20, 2004
  4. Bengt Richter
    Replies:
    6
    Views:
    473
    Juha Autero
    Aug 19, 2003
  5. jack
    Replies:
    4
    Views:
    588
Loading...

Share This Page