Python Forum
How to understand the byte notation in python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to understand the byte notation in python3
Hey, I am trying to learn low-level stuff in py3 for some crypto project, and it is pretty hard so far.

In [63]: int(b'\x12AY5'.hex(),16)
Out[63]: 306272565 #Value of some weird byte 

In [64]: int('11111111', 2) #Highest possible val of one byte afaik
Out[64]: 255

So the first question is how this byte can have such a big value, what is this notation 'AY'? I am also dealing with notations like 'x9b;', 'x16p0?', 'x17D%', and similar.

And the second question is why this gives me another value for the same byte, seems like this hex() method was wrong somehow?:
In [65]: int.from_bytes(b'\x12AY5', byteorder='little')
Out[65]: 895041810
#In [63]: int(b'\x12AY5'.hex(),16)
#Out[63]: 306272565 
It's not a single byte, it's a bytes object which can hold several bytes. When printed, if the byte is in the ASCII range, it shows the ASCII character. If the byte is not in ASCII, it shows the value with 2 hexadecimal digits. Your object has 4 bytes inside. As an example, to break down your string into the component bytes in character, decimal, and hex form:

>>> for byte in b'\x12AY5':
...  print(f"{repr(chr(byte)):>6s} - {byte} - {byte:x}")
'\x12' - 18 - 12
   'A' - 65 - 41
   'Y' - 89 - 59
   '5' - 53 - 35
As it's not a single byte, you have a choice of what order if you want to assemble it into a single 32-bit decimal.

12 41 59 35 => 306272565
35 59 41 12 => 895041810
blackknite likes this post
Okay, thank you for your answer.
You have made it very clear but this points me to another issue - How I am supposed to know if some byte is a part of another one? It looks tricky:

In [75]: bstr = b'r\xd4M\xdb\xbd\xddp' 

In [76]: [x for x in bstr]
Out[76]: [114, 212, 77, 219, 189, 221, 112] #MESS
Is there some pythonic way to list all byte-objects one by one with the correct int value?
I think the way you've done it above is fine. Bytes were the original str class in python before python3, so it defaults to printing the string representation. If you don't mind viewing it as hex then maybe:

>>> bstr = b'r\xd4M\xdb\xbd\xddp'
>>> bstr.hex(sep=",")
But your listcomp to show it as decimal is perfectly good.

Possibly Related Threads…
Thread Author Replies Views Last Post
  ''.join and start:stop:step notation for lists ringgeest11 1 535 Apr-08-2021, 04:29 PM
Last Post: BashBedlam
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 442 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 3,465 Sep-18-2020, 10:10 PM
Last Post: tienttt
  Simple question concerning python dot notation. miner_tom 1 656 Mar-24-2020, 05:20 PM
Last Post: buran
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 24,581 Feb-08-2020, 06:58 PM
Last Post: karkas
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 2,317 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  how to implement the .mymethod() notation of Python Pedroski55 4 1,633 Apr-22-2019, 10:24 PM
Last Post: Gribouillis
  4 byte hex byte swap from binary file medievil 7 14,773 May-08-2018, 08:16 AM
Last Post: killerrex
  Object madness - JSON Notation confusion execsys 4 2,003 May-03-2018, 08:56 AM
Last Post: buran
  printing engineering notation Skaperen 1 2,885 Sep-28-2017, 03:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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