![]() |
|
[Tut] Convert Bytes To Floating Point Numbers - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Convert Bytes To Floating Point Numbers (/thread-99125.html) |
[Tut] Convert Bytes To Floating Point Numbers - xSicKxBot - 04-02-2022 Convert Bytes To Floating Point Numbers Summary: The Code: structCode: bytesCode: floatingTable of Contents
Problem: How to convert bytes to floating point numbers? A quick look at the solution: ![]() [b]A Brief Introduction to Struct[/b] The Code: struct
➦ 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.
FINXTER ACADEMY SUBSCRIBE |