Python Forum
Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects (/thread-41447.html)



Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects - new_coder_231013 - Jan-17-2024

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.


RE: Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects - Gribouillis - Jan-17-2024

(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.


RE: Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects - new_coder_231013 - Apr-06-2024

Thanks!