hex question

S

Sneaky Wombat

Why is python turning \x0a into a \n ?

In [120]: h='\x0a\xa8\x19\x0b'

In [121]: h
Out[121]: '\n\xa8\x19\x0b'


I don't want this to happen, can I prevent it?
 
E

Emile van Sebille

On 6/25/2010 1:20 PM Sneaky Wombat said...
Why is python turning \x0a into a \n ?

In [120]: h='\x0a\xa8\x19\x0b'

In [121]: h
Out[121]: '\n\xa8\x19\x0b'


I don't want this to happen, can I prevent it?


It's not happening. What you're seeing is the representation of the
four bytes, and \x0a _is_ \n and python displays the common form to
assist interpretation.

What you can do is write a display function to suit your needs if it
makes a difference.

\x48\x54\x48\x2c

Emile
 
E

Ethan Furman

Sneaky said:
Why is python turning \x0a into a \n ?

In [120]: h='\x0a\xa8\x19\x0b'

In [121]: h
Out[121]: '\n\xa8\x19\x0b'


I don't want this to happen, can I prevent it?

'\x0a' == '\n'
 
M

Matt McCredie

Sneaky Wombat said:
Why is python turning \x0a into a \n ?

In [120]: h='\x0a\xa8\x19\x0b'

In [121]: h
Out[121]: '\n\xa8\x19\x0b'

I don't want this to happen, can I prevent it?

'h' is an ascii string. The ascii encoding for '\n' is the number(byte) 0x0A.
When you type '\x0a' you are entering the ascii code directly.
'0xa'

Python doesn't know that you entered the values using the '\xXX' syntax, it just
knows that the string contains a byte with that value. When it prints it back
out, it will print out the corresponding symbol.

Any character that has a reasonable ascii representation will show up as that
symbol when it (or its repr) is printed.
'abcdef'

If you are interested in printing the hex values, you could so something like
this:
.... print "0x%02x" % ord(c),
....
0x0a 0xa8 0x19 0x0b


Matt
 
D

Dave Angel

Sneaky said:
Why is python turning \x0a into a \n ?

In [120]: h='\x0a\xa8\x19\x0b'

In [121]: h
Out[121]: '\n\xa8\x19\x0b'


I don't want this to happen, can I prevent it?
You don't say what you do want. Currently, you have a literal that
describes four characters. Were you trying for 7 ? If so, try escaping
the first backslash.
h2 = '\\x0a\xa8\x19\x0b'

On the other hand, maybe you're really trying for four, and think that
the first one is different than you intended. It's not. You have to
realize that the interactive interpreter is using repr() on that string,
and a string representation chooses the mnemonic versions over the hex
version. There are frequently several ways to represent a given
character, and once the character has been stored, repr() will use its
own judgment on how to show it.

For a simpler example,
b = '\x41\x42c'
b
will display ABc

'x41' is just another way of saying 'A'. And '\0a' is just another
way of saying '\n' or newline.

DaveA
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top