Python Forum
Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects
#1
Hello,

Sorry in advance for the likely basic nature of this question. I'm wondering how the "int.from_bytes" method returns the output that it does when it's converting input other than the escaped parts of bytes objects (see below for some examples).

These examples I understand - they're simply converting hexadecimals to decimal integers.

print(int.from_bytes(b'\x0f', "little"))
Output:
15
print(int.from_bytes(b'\xc0\xff\xee', "big"))
Output:
12648430
print(int.from_bytes(b'\xc0\xff\xee', "little"))
Output:
15663040
These examples I don't understand:

print(int.from_bytes(b'1', "big"))
Output:
49
print(int.from_bytes(b'Hello world', "big"))
Output:
87521618088882671231069284
print(int.from_bytes(b'Helloworld', "big"))
Output:
341881320660071462628452
Does it have something to do with the base system of the input and / or output - such as the particular base system the method assumes the input is expressed in? Or the base system it uses for the output? What am I missing here?

Thanks so much in advance for any help and have a great day.
Reply
#2
(Jan-17-2024, 08:54 PM)new_coder_231013 Wrote: Does it have something to do with the base system of the input and / or output - such as the particular base system the method assumes the input is expressed in? Or the base system it uses for the output? What am I missing here?
The key thing to understand about the bytes type in Python is that while Python prints b'Hello world', the bytes object is actually an array of small integers
>>> list(b'Hello world')
[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
>>> a = b'Hello world'
>>> a[0]
72
>>> a[1]
101
The printed version uses Ascii encoding for printable characters.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Does XLRD have a method to extract embedded objects (pdf) from an excel sheet? matthewme 1 2,513 Jun-13-2019, 10:00 PM
Last Post: Larz60+
  replace bytes with other byte or bytes BigOldArt 1 10,652 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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