Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

29. Imagine you just received a bytes object b from a sender over the network. T

ID: 3749770 • Letter: 2

Question

29. Imagine you just received a bytes object b from a sender over the network. The bytes object holds the bits with hexadecimal shorthand 42 41. If we run the command int.from_bytes (b, 'big'), we get the python int 16961. If we run the command b.decode( ASCII'), we get the python str 'BA' Describe how you could determine whether it is a raw binary number or ASCII text that is meant to be stored in the bytes object. In other words, did the user mean to send the int or the str? Be creative - there are multiple ways you might know. But you must think outside the bytes object. The goal is critical thinking, not remembering some fact from class or the book.

Explanation / Answer

In bytes object, we can store strings in their ASCII format or integers in raw binary format. ASCII is a way to represent characters in computers. So, every character is given a code. And that code is always 1 byte long. So, if we store something in bytes object, one byte will store a code that can be used to represent an ASCII character. That code can even be converted into an integer. So, it is totally up to the coder what he/she wants to store.

For e.g., if a byte stores hexadecimal shorthand 42, then this means the byte stores (0100 0010) which can be used to represent a 'B' character or it can be converted to an integer which will be 66.

So, if a byte object stores two bytes, 42 41 then we can convert 42 to 'B' and 41 to 'A'. And that will give us 'BA'. Else, if we convert the binary number to decimal format, it will be equal to 16961.

When we convert one format to another, without losing information, it is known as encoding. And the process to revert back is called decoding. A character takes one or more bytes to be stored if we work in python. Similarly, an integer takes 4 bytes when we store it in memory. To reduce the amount of memory used, we convert them to bytes.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote