04-02-2022, 06:00 PM
Convert Bytes To Floating Point Numbers
Summary: The module in Python, taken from the C programming language, lets you convert to and from point numbers.
Table of Contents
Problem: How to convert bytes to floating point numbers?
A quick look at the solution:
![[Image: image-200.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-200.png)
[b]A Brief Introduction to Struct[/b]
The module has three main methods for data conversion:
➦ You can use the method to convert bytes to floating point numbers. The method accepts data format and the byte to be converted.
➦ On the other hand, the method helps in converting data types to bytes.
Where stands for floats. As you will see in other parts of this tutorial, the module handles different data types such as characters, integers and floats. But before that, you should understand , numbers, and the concept of .
[b]Definition Of Bytes And Floating Point Numbers[/b]
This section focuses on the roots of bytes and data formats.
Unlike humans that represent numbers in base 10 (0 to 9 digits), computers understand the language of 1s and 0s. A pair of 1 and 0 is a binary digit, shortened as a bit. So, the computer converts data into a series of 1s and 0s (bits) before storing them in the memory.
Likewise, non-numerical data get stored as bytes. For instance, a character occupies 1 byte of memory. An array of characters forms a string.
Format |
Type in C programming language |
size in bytes
|
char |
1
|
signed char |
1
|
unsigned char |
1
|
_Bool |
1
|
short |
2
|
unsigned short |
2
|
int |
4
|
unsigned int |
4
|
long |
4
|
unsigned long |
4
|
long long |
8
|
unsigned long long |
8
|
char[] |
|
float |
4
Now that you understand how a computer interprets various data types, it would be best to learn how the struct module uses them to convert bytes to floating point numbers. Since struct has been taken from the C programming language, hence, a deeper understanding of how it works in C is crucial.
[b]Struct Padding vs Packing[/b]
In the C programming language, a is a user-defined data type. Unlike other variables, a struct combines different data types in a structure. Here is an example.
We are creating a blueprint of a Fruit with a character, and integer. We can create a banana from the Fruit model, assigning it b as and 23 as .
Printing the size of each attribute,
we get the result as
The size of fruit1 should be (1 + 4 =) 5 bytes, right? Let’s check.
It turns out our computer uses more memory to store smaller data quantities. That happens due to a concept called padding.
CPU reads 4 bytes of data per cycle. For the first cycle, it adds three bytes of space to the available (1) byte and returns the result. Next, it finds 4 bytes and returns them. In total, it records (4 + 4 =) 8 bytes.
Padding wastes memory while reducing CPU cycles. Struct packing comes in to store more bytes in less memory.
We include header with function of value 1 to reduce the memory wastage when compiling and assembling data. This time around, the struct size is what we expect: 1 byte + 4 bytes = 5 bytes.
The key takeaway is that struct is a C programming language data structure for storing user-defined data types. Unlike other data types, it combines a continuous stream of different data types. The various data types consume more memory due to padding, something we can control using struct packing.
We can apply the concept of struct to convert data types to bytes in Python, as illustrated below.
[b]How To Use The Struct Module In Python[/b]
Unlike C, which speaks to the memory through data types and compiling, Python is a high-level, dynamically-typed programming language. Most operations occur through modules (classes) that get translated and compiled to produce bytes. One such module is the module.
The struct module has three methods:, , and . The pack method accepts two parameters: format and data to be converted to bytes. Like the struct blueprint in C, the format part of the pack function in Python accepts various data types. For example,
This means converting integer 2, integer 4 and float 7.68 to a stream of bytes. stands for an integer while represents floats.
You can represent repeating integers by a numeral. For example, can map to . Also, you can separate the data types with a space. For example, is another representation for .
We can check the format size by importing the module,
and using its method.
In the same way, we can convert a floating point number into bytes. Assume we want to convert 3.76 to bytes. We can do that using the following code.
Output:
Here, stands for . The other parts may differ as per computer depending on the memory address system and endianness. Let’s now find floating point numbers from bytes.
[b]Convert Bytes To Floating Point Numbers[/b]
The unpack function accepts format, and the byte stream then converts it to a floating point number. For example, we can decode as follows.
The result is a tuple containing a floating point number with a massive number of decimal points.
We can extract the result by surrounding the input with square brackets.
The result of printing the output is.
The extended decimal output from a reduced input size shows the essence of scientific notation in computing. It also proves the reason for the preference of floating point numbers over integers.
Apart from efficiency, handling floating point numbers comes with speed since much work has gone into building floating point numbers over the years.
[b]Conclusion[/b]
The struct module with its method helps convert bytes to floating point numbers. It would help to understand other methods such as and because, from them, you can generate bytes from various data types.
Another way to ease handling the conversion is understanding the ins and outs of the struct module, as explained in this tutorial.
FINXTER ACADEMY
SUBSCRIBE
Summary: The
Code:
structCode:
bytesCode:
floatingTable of Contents
- A Brief Introduction to Struct
- Struct Padding vs Packing
- How To Use The Struct Module In Python
- Convert Bytes To Floating Point Numbers
- Conclusion
Problem: How to convert bytes to floating point numbers?
A quick look at the solution:
![[Image: image-200.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-200.png)
[b]A Brief Introduction to Struct[/b]
The
Code:
struct- ,Code:
unpack()
- Code:
pack()
- .Code:
calcsize()
➦ You can use the
Code:
unpack()Code:
struct.unpack("f", <byte>)➦ On the other hand, the
Code:
pack()Code:
struct.pack("f", <floating point number>)Where
Code:
fCode:
structCode:
bytesCode:
floating pointCode:
structs[b]Definition Of Bytes And Floating Point Numbers[/b]
This section focuses on the roots of bytes and data formats.
Unlike humans that represent numbers in base 10 (0 to 9 digits), computers understand the language of 1s and 0s. A pair of 1 and 0 is a binary digit, shortened as a bit. So, the computer converts data into a series of 1s and 0s (bits) before storing them in the memory.
Likewise, non-numerical data get stored as bytes. For instance, a character occupies 1 byte of memory. An array of characters forms a string.
Format |
Type in C programming language |
size in bytes
Code:
cchar |
1
Code:
bsigned char |
1
Code:
Bunsigned char |
1
Code:
?_Bool |
1
Code:
hshort |
2
Code:
Hunsigned short |
2
Code:
iint |
4
Code:
Iunsigned int |
4
Code:
llong |
4
Code:
Lunsigned long |
4
Code:
qlong long |
8
Code:
Qunsigned long long |
8
Code:
schar[] |
Code:
ffloat |
4
Now that you understand how a computer interprets various data types, it would be best to learn how the struct module uses them to convert bytes to floating point numbers. Since struct has been taken from the C programming language, hence, a deeper understanding of how it works in C is crucial.
[b]Struct Padding vs Packing[/b]
In the C programming language, a
Code:
structCode:
struct Fruit { char firstLetter; int total;
};We are creating a blueprint of a Fruit with a
Code:
firstLetterCode:
totalCode:
firstLetterCode:
totalCode:
struct Fruit fruit1 = { 'b', 23 };Printing the size of each attribute,
Code:
printf("firstLetter is %d bytes and total is %d bytes \n", sizeof(fruit1.firstLetter),sizeof(fruit1.number));we get the result as
Code:
firstLetter is 1 bytes and total is 4 bytesThe size of fruit1 should be (1 + 4 =) 5 bytes, right? Let’s check.
Code:
Size of fruit1 is 8 bytesIt turns out our computer uses more memory to store smaller data quantities. That happens due to a concept called padding.
CPU reads 4 bytes of data per cycle. For the first cycle, it adds three bytes of space to the available (1) byte and returns the result. Next, it finds 4 bytes and returns them. In total, it records (4 + 4 =) 8 bytes.
Padding wastes memory while reducing CPU cycles. Struct packing comes in to store more bytes in less memory.
Code:
#include <stdio.h>
#pragma pack(1) struct Fruit { char firstLetter; int number;
}; int main () { struct Fruit fruit1 = { 'b', 23 }; // printf("firstLetter is %d bytes and total is %d bytes \n", sizeof(fruit1.firstLetter),sizeof(fruit1.number)); printf("Size of fruit1 is %d bytes \n", sizeof(fruit1)); return 0;
}We include
Code:
#pragma pack(1)Code:
packCode:
Size of fruit1 is 5 bytesThe key takeaway is that struct is a C programming language data structure for storing user-defined data types. Unlike other data types, it combines a continuous stream of different data types. The various data types consume more memory due to padding, something we can control using struct packing.
We can apply the concept of struct to convert data types to bytes in Python, as illustrated below.
[b]How To Use The Struct Module In Python[/b]
Unlike C, which speaks to the memory through data types and compiling, Python is a high-level, dynamically-typed programming language. Most operations occur through modules (classes) that get translated and compiled to produce bytes. One such module is the
Code:
structThe struct module has three methods:
Code:
pack()Code:
unpack()Code:
calcsize()Code:
struct.pack('iif', 2, 4, 7.68)This means converting integer 2, integer 4 and float 7.68 to a stream of bytes.
Code:
iCode:
fYou can represent repeating integers by a numeral. For example,
Code:
iiiCode:
3iCode:
3i fCode:
3ifWe can check the format size by importing the module,
Code:
import structand using its
Code:
calcsize()Code:
struct.calcsize('3if')In the same way, we can convert a floating point number into bytes. Assume we want to convert 3.76 to bytes. We can do that using the following code.
Code:
byteData = struct.pack('f', 3.76) print(byteData)Output:
Code:
b'\xd7\xa3p@'Here,
Code:
bCode:
bytes[b]Convert Bytes To Floating Point Numbers[/b]
The unpack function accepts format, and the byte stream then converts it to a floating point number. For example, we can decode
Code:
b'\xd7\xa3p@'Code:
byteData = struct.unpack('f', b'\xd7\xa3p@') print(byteData)The result is a tuple containing a floating point number with a massive number of decimal points.
Code:
(3.759999990463257,)We can extract the result by surrounding the input with square brackets.
Code:
[byteData] = struct.unpack('f', b'\xd7\xa3p@')The result of printing the output is
Code:
3.759999990463257The extended decimal output from a reduced input size shows the essence of scientific notation in computing. It also proves the reason for the preference of floating point numbers over integers.
Apart from efficiency, handling floating point numbers comes with speed since much work has gone into building floating point numbers over the years.
[b]Conclusion[/b]
The struct module with its
Code:
unpack()Code:
pack()Code:
calcsizeAnother way to ease handling the conversion is understanding the ins and outs of the struct module, as explained in this tutorial.
- Recommended Reads:
FINXTER ACADEMY
SUBSCRIBE

