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

J. Clifford Dyer jcd at sdf.lonestar.org
Sat May 24 19:25:46 EDT 2008


On Sat, 2008-05-24 at 15:59 -0700, zxo102 wrote:
> But this is not "\xED\x6F\x3C\x01".  I need it for
> struct.unpack('f',"\xED\x6F\x3C\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... at gmx.net>
> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > [ zxo102 <zxo... at gmail.com> ]
> >
> > >    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'.decode('hex')
> > Out[1]: '\xedo<\x01'
> >
> > - --
> > Freedom is always the freedom of dissenters.
> >                                       (Rosa Luxemburg)
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v2.0.9 (GNU/Linux)
> >
> > iEYEARECAAYFAkg4mtEACgkQn3IEGILecb7W6ACeNwr/vavkaXluvc0zeSa4cy1N
> > YFIAoJjMsrRcLhqAPRxKktUqt7miMTrs
> > =jxll
> > -----END PGP SIGNATURE-----
> 
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list