Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\x3C\x01" inpython coding?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zxo102

    Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\x3C\x01" inpython coding?

    Hi,
    how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
    "\xED\x6F\x3C\x 01" in python coding?
    When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
    the error information : invalid \x escape.
    Thanks.

    ouyang
  • Sebastian 'lunar' Wiesner

    #2
    Re: Hexadecimal: how to convert 'ED6F3C01' to "\xED\x6F\ x3C\x01" in python coding?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    [ zxo102 <zxo102@gmail.c om]
    how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
    "\xED\x6F\x3C\x 01" in python coding?
    When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
    the error information : invalid \x escape.
    [1]--'ED6F3C01'.deco de('hex')
    Out[1]: '\xedo<\x01'

    - --
    Freedom is always the freedom of dissenters.
    (Rosa Luxemburg)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)

    iEYEARECAAYFAkg 4mtEACgkQn3IEGI Lecb7W6ACeNwr/vavkaXluvc0zeSa 4cy1N
    YFIAoJjMsrRcLhq APRxKktUqt7miMT rs
    =jxll
    -----END PGP SIGNATURE-----

    Comment

    • zxo102

      #3
      Re: Hexadecimal: how to convert 'ED6F3C01' to &quot;\xED\x6F\ x3C\x01&quot; inpython coding?

      But this is not "\xED\x6F\x3C\x 01". I need it for
      struct.unpack(' f',"\xED\x6F\x3 C\x01") to calculate the decimal value
      (IEEE 754).
      Any other suggestions?

      ouyang

      On 5ÔÂ25ÈÕ, ÉÏÎç6ʱ46·Ö, Sebastian 'lunar' Wiesner <basti.wies...@ gmx.net>
      wrote:
      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1
      >
      [ zxo102 <zxo...@gmail.c om]
      >
      how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
      "\xED\x6F\x3C\x 01" in python coding?
      When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
      the error information : invalid \x escape.
      >
      [1]--'ED6F3C01'.deco de('hex')
      Out[1]: '\xedo<\x01'
      >
      - --
      Freedom is always the freedom of dissenters.
      (Rosa Luxemburg)
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v2.0.9 (GNU/Linux)
      >
      iEYEARECAAYFAkg 4mtEACgkQn3IEGI Lecb7W6ACeNwr/vavkaXluvc0zeSa 4cy1N
      YFIAoJjMsrRcLhq APRxKktUqt7miMT rs
      =jxll
      -----END PGP SIGNATURE-----

      Comment

      • zxo102

        #4
        Re: Hexadecimal: how to convert 'ED6F3C01' to &quot;\xED\x6F\ x3C\x01&quot; inpython coding?

        On 5ÔÂ25ÈÕ, ÉÏÎç6ʱ59·Ö, zxo102 <zxo...@gmail.c omwrote:
        But this is not "\xED\x6F\x3C\x 01". I need it for
        struct.unpack(' f',"\xED\x6F\x3 C\x01") to calculate the decimal value
        (IEEE 754).
        Any other suggestions?
        >
        ouyang
        >
        On 5ÔÂ25ÈÕ, ÉÏÎç6ʱ46·Ö, Sebastian 'lunar' Wiesner<basti.w ies...@gmx.net>
        wrote:
        >
        >
        >
        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1
        >
        [ zxo102 <zxo...@gmail.c om]
        >
        how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
        "\xED\x6F\x3C\x 01" in python coding?
        When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
        the error information : invalid \x escape.
        >
        [1]--'ED6F3C01'.deco de('hex')
        Out[1]: '\xedo<\x01'
        >
        - --
        Freedom is always the freedom of dissenters.
        (Rosa Luxemburg)
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v2.0.9 (GNU/Linux)
        >
        iEYEARECAAYFAkg 4mtEACgkQn3IEGI Lecb7W6ACeNwr/vavkaXluvc0zeSa 4cy1N
        YFIAoJjMsrRcLhq APRxKktUqt7miMT rs
        =jxll
        -----END PGP SIGNATURE------ Òþ²Ø±»ÒýÓÃÎÄ×Ö -
        >
        - ÏÔʾÒýÓõÄÎÄ×Ö -
        Hash: SHA1
        I got it. Just simply use it like
        struct.unpack(' f',"ED6F3C01".d ecode('hex')). It works now.

        Thank you very much.
        ouyang

        Comment

        • J. Clifford Dyer

          #5
          Re: Hexadecimal: how to convert 'ED6F3C01' to &quot;\xED\x6F\ x3C\x01&quot;in python coding?

          On Sat, 2008-05-24 at 15:59 -0700, zxo102 wrote:
          But this is not "\xED\x6F\x3C\x 01". I need it for
          struct.unpack(' f',"\xED\x6F\x3 C\x01") to calculate the decimal value
          (IEEE 754).
          Any other suggestions?
          >
          ouyang
          >
          In fact it is exactly the same string. The repr of a string always
          substitutes ascii values for their numeric equivalent, but those bytes
          are still the underlying representation of the string.
          >>[hex(ord(c)) for c in "\xefo<\x01 "]
          ['0xef', '0x6f', '0x3c', '0x1']

          Cheers,
          Cliff
          On 5月25日, 上午6时46分 , Sebastian 'lunar' Wiesner <basti.wies...@ gmx.net>
          wrote:
          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          [ zxo102 <zxo...@gmail.c om]
          how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
          "\xED\x6F\x3C\x 01" in python coding?
          When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
          the error information : invalid \x escape.
          [1]--'ED6F3C01'.deco de('hex')
          Out[1]: '\xedo<\x01'

          - --
          Freedom is always the freedom of dissenters.
          (Rosa Luxemburg)
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v2.0.9 (GNU/Linux)

          iEYEARECAAYFAkg 4mtEACgkQn3IEGI Lecb7W6ACeNwr/vavkaXluvc0zeSa 4cy1N
          YFIAoJjMsrRcLhq APRxKktUqt7miMT rs
          =jxll
          -----END PGP SIGNATURE-----
          >
          --
          http://mail.python.org/mailman/listinfo/python-list

          Comment

          • John Machin

            #6
            Re: Hexadecimal: how to convert 'ED6F3C01' to &quot;\xED\x6F\ x3C\x01&quot;in python coding?

            zxo102 wrote:
            Hi,
            how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
            "\xED\x6F\x3C\x 01" in python coding?
            If by "in python coding" you mean "in Python source code", then just
            type it in with \x in front of each pair of hex digits, like you did above.

            However if you mean e.g. how to change a data string x into a data
            string y, something like this is what you want
            >>import binascii
            >>x = 'ED 6F 3C 01'
            >>y = binascii.unhexl ify(x.replace(' ', ''))
            >>y
            '\xedo<\x01'

            .... which is correct ('o' == '\x6f' and '<' == '\x3c'); see below:
            >>' '.join(['%02x' % ord(c) for c in y])
            'ed 6f 3c 01'
            >>>len(y)
            4

            Does (len(y) == 4) surprise you?
            When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
            the error information : invalid \x escape.
            It's rather difficult to guess what you mean here ... insert how many
            '\x'? where?? what gave you the error information??? Consider showing us
            a copy/paste of exactly what you did and what was the response, like I
            did above.


            HTH,
            John

            Comment

            • John Machin

              #7
              Re: Hexadecimal: how to convert 'ED6F3C01' to &quot;\xED\x6F\ x3C\x01&quot;in python coding?

              Sebastian 'lunar' Wiesner wrote:
              -----BEGIN PGP SIGNED MESSAGE-----
              Hash: SHA1
              >
              [ zxo102 <zxo102@gmail.c om]
              >
              > how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
              >"\xED\x6F\x3C\ x01" in python coding?
              >When I take 'ED6F3C01' as a string and insert '\x' into it, I just got
              >the error information : invalid \x escape.
              >
              [1]--'ED6F3C01'.deco de('hex')
              Out[1]: '\xedo<\x01'
              >
              FWIW the 'hex' codec is just a big fat Python-coded wrapper around the
              routines in the C-coded binascii module; here's the evidence:
              >>'123'.decode( 'hex')
              Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
              File "c:\python25\li b\encodings\hex _codec.py", line 42, in hex_decode
              output = binascii.a2b_he x(input)
              TypeError: Odd-length string

              Comment

              Working...