By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi,
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.
Thanks.

ouyang
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ zxo102 <zx****@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-----
Jun 27 '08 #2

P: n/a
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

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

[ zxo102 <zxo...@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-----
Jun 27 '08 #3

P: n/a
On 5月25日, 上午6时59分, zxo102 <zxo...@gmail.comwrote:
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

On 5月25日, 上午6时46分, Sebastian 'lunar' Wiesner<basti.wies...@gmx.net>
wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[ zxo102 <zxo...@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------ 隐藏被引用文字 -

- 显示引用的文字 -
Hash: SHA1
I got it. Just simply use it like
struct.unpack('f',"ED6F3C01".decode('hex')). It works now.

Thank you very much.
ouyang
Jun 27 '08 #4

P: n/a
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...@gmx.net>
wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ zxo102 <zxo...@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
Jun 27 '08 #5

P: n/a
zxo102 wrote:
Hi,
how to change the hexadecimal 'ED6F3C01' (or 'ED 6F 3C 01') to
"\xED\x6F\x3C\x01" 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.unhexlify(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
Jun 27 '08 #6

P: n/a
Sebastian 'lunar' Wiesner wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ zxo102 <zx****@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'
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\lib\encodings\hex_codec.py", line 42, in hex_decode
output = binascii.a2b_hex(input)
TypeError: Odd-length string
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.