468,285 Members | 1,594 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,285 developers. It's quick & easy.

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\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
6 4415
-----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
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
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
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
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
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.

Similar topics

9 posts views Thread by FalkoG | last post: by
10 posts views Thread by pavithra.eswaran | last post: by
2 posts views Thread by akash deep batra | last post: by
1 post views Thread by Charles | last post: by
15 posts views Thread by jaks.maths | last post: by
7 posts views Thread by elliotng.ee | last post: by
6 posts views Thread by Andrea | last post: by
5 posts views Thread by sweeet_addiction16 | last post: by
11 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.