
This section is talking about non-negative integers only. Thinking about it another way, 65536 is the number of combinations of 1s and 0s you can have in a 16-bit number. The more bytes you use to represent an integer, the larger the range of integers you can represent.
So what happens if you want to store number larger than 255? Like 256? In that case, you need to use a second byte to store the additional value. In decimal, these numbers go from 0 to 255. These sequences of bytes are what we’ll send across the network to send integer values to other systems.Ī single byte (in this context well define a byte to be the usual 8 bits) can encode binary values from 00000000 to 11111111. Let’s look at how integers are represented as sequences of bytes.
In this section we’ll dive deep into how an integer can be represented by a sequence of individual bytes.
Python offers built-in functionality for converting integers to sequences of bytes and back again. Big-Endian and Little-Endian are two different ways of ordering those sequences of bytes. We’ll convert sequences of bytes back into integers when we receive them over the network. We’ll convert integers to sequences of bytes before we transmit them over the network. Integers can be represented by sequences of bytes. How to convert a sequence of bytes to a number in Python. How to convert a number to a sequence of bytes in Python. How numbers are represented by sequences of bytes. Convert byte sequences back into integers. Binary representation is more compact and saves bandwidth.īut the network can only send and receive bytes! How can we convert arbitrary numbers to single bytes? Sure we could just convert the numbers to strings, but this is more wasteful than it needs to be. But now we want to do something else: we want to transfer binary integer data. We’ve done some work transmitting text over the network.