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

Z

zxo102

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
 
S

Sebastian 'lunar' Wiesner

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

[ zxo102 said:
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-----
 
Z

zxo102

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

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

[ zxo102 said:
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-----
 
Z

zxo102

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[ zxo102 <[email protected]> ]
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
 
J

J. Clifford Dyer

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

A string is made up of a list of bytes, and when you use hexadecimal
character references, the \ and the x do not represent bytes on their
own, hence they cannot be manipulated as though they were individual
characters of the string. The sequence \xNN is one byte, as a unit. It
cannot be divided. So if you try:

'\x%s" % 'ef'

you will get an error, because you are substituting a two-byte string
'ef', where in fact you only wanted to complete the one byte in
question. \x can never be separated from the two hexadecimal digits
that follow it. They must be treated as a unit. To get the two-byte
string 'ef' converted to the one byte character '\xef', you have to find
a way to get the numerical value of ef, and create a character of that
value. The first part is done as follows:

int('ef', 16)

which means "get the integer value of 'ef', where 'ef' is represented in
base 16. In order to get the byte of hexadecimal numeric value 0xef,
you use the chr() function:

'\xef' == chr(int('ef', 16))

Or if you are working with unicode, use unichr() instead of chr().

If you always treat \xNN escape sequences as indivisible, you won't go
wrong.

Cheers,
Cliff
 
J

J. Clifford Dyer

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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[ zxo102 said:
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-----
 
J

John Machin

zxo102 said:
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
'\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
 
J

John Machin

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

[ zxo102 said:
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:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top