c - weird hexadecimal representation in python \xhhK

Today I was playing a little bit with encoding variables in c style to get easy communication to a necessary c-program.

I read https://docs.python.org/2/library/struct.html

I know that \x starts 2 digit hex representation but for a few numbers I get:

from struct import *

datum=239179

buf = pack(">Q", datum)

buf

'\x00\x00\x00\x00\x00\x03\xa6K'

What ist \xa6K? \xa6 is the valid form.

Unpacking this variable works totally fine, so it seems a legit way of hex but why? For a similar problem a friend wrote a go-program, which gives him for the same number

00 00 00 00 00 03 a6 4b . Now if we check the hex value of K it is 4b.

tldr;

Why is \xa6K the same as \xa6\x4b ?

Thanks for your help :)

Thanks for this solution , i feel a little bit stupid :D

网友答案:

struct.pack returns a str object (bytes in python3). Strings choose to represent non-printing characters using hex codes ('\xa6' for instance). However, the byte corresponding to '\x4b' is a printable character, so the string uses that instead.