Python Forum
Python Struct Question, decimal 10, \n and \x0a.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Struct Question, decimal 10, \n and \x0a.
#1
Can someone explain why the decimal value 10, when in Python Struct is showing as \n, and not \x0a?

import struct

marDept5 = struct.pack("b", 10)
print(marDept5)
marDept5a = struct.pack("h", 10)
print(marDept5a)
marDept5b = struct.pack("i", 10)
print(marDept5b)

print("this is decimal 10, struct.pack("b", 10))
print("this is decimal 11, struct.pack("b", 11))
print("this is decimal 12, struct.pack("b", 12))
Output:
b'\n' b'\n\x00' b'\n\x00\x00\x00' this is decimal 10, b'\n' this is decimal 11, b'\x0b' this is decimal 12, b'\x0c'
Why is decimal 10 appearing as \n and not \x0a ? It is after all hex value \x0a. I tried in multiple IDEs and online python compilers and they all return it as value \n. Just looking for an explanation to why?
Reply
#2
(Aug-11-2023, 06:39 AM)3python Wrote: Why is decimal 10 appearing as \n and not \x0a ? It is after all hex value \x0a.

Python replaces known ASCII characters with their representation as Escape Sequences.

Documentation: https://docs.python.org/3/reference/lexi...l#literals

These special characters are not all printable. The newline character is only shown in the representation of a str/bytes object. Printing a str -> Newline characters creates a newline. Printing bytes with newline -> Newline Character is not interpreted.

If you need a different representation of binary data, then try binascii.hexlify and binascii.unhexlify. Instead of a str with mixed hexadecimal escape sequences and ascii, you get a hexdecimal string.
Gribouillis and 3python like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thank you DeaD_EyE! This makes sense now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,346 May-07-2021, 12:39 AM
Last Post: MDRI
  JS Buffer.from VS struct.pack DreamingInsanity 3 2,477 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity
  struct.unpack failed Roro 2 3,368 Jun-13-2020, 05:28 PM
Last Post: DreamingInsanity
  Pack integer values as single bytes in a struct bhdschmidt 3 2,354 Jun-09-2020, 09:23 PM
Last Post: bhdschmidt
  struct.decode() and '\0' deanhystad 1 3,240 Apr-09-2020, 04:13 PM
Last Post: TomToad
  python decimal scale precision massimo_m 5 5,447 Aug-22-2019, 11:47 AM
Last Post: perfringo
  How to remove empty struct from matlab file in python? python_newbie09 0 2,392 Jun-25-2019, 12:13 PM
Last Post: python_newbie09
  testing for Decimal w/o importing decimal every time Skaperen 7 4,488 May-06-2019, 10:23 PM
Last Post: Skaperen
  Python 2 to Python 3 Struct Issue robin73 5 6,374 Jun-16-2018, 10:47 PM
Last Post: robin73
  Error in Python 3.6 and how to limit decimal places Raptor88 6 9,888 Mar-18-2017, 05:02 AM
Last Post: Raptor88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020