0

I am using bytes in python to convert following integers into bytes:

>>> b = bytes([192, 168, 10, 254])

I get the following output: b'\xc0\xa8\n\xfe'

My question, why does the int 10 is represented as \n and not hex value of '0xa'. Looking at the output, 192, 168 and 254 all gets represented as their equilvalent hex values. Why not 10 is represented by its hex value.

Thanks

frank
  • 59
  • 2
  • 9
  • Because 10 is the ascii value of a newline character, and `bytes` shows its values as characters when appropriate. – khelwood Nov 04 '20 at 23:46
  • The internal representation is the same (try `print("\n" == "\x0a")`). If you want to get a pure hex output you may try something like `print(*map(hex, [192, 168, 10, 254]))`. – Selcuk Nov 04 '20 at 23:50
  • So looking at the extended ascii table 168 represents an upside down question mark, but i am assuming this is not common so it just outputs the equalivalent hex of value '0xa8'. – frank Nov 04 '20 at 23:57

0 Answers0