Read and write single precision floating point in Python

It seems that Python does not support single precision floating point as a language specification, but since it was necessary to input and output the setting value of the hardware to which the script is linked with single precision fixed decimal point, convert it through NumPy. I made it.

For the time being, make a note so that you do not forget it.

Magic

Because it uses NumPy

import numpy

Enter the value of float32

Assign each 4-byte float32 value (12 bytes in total) contained in the string s to the variable

nums = numpy.fromstring(s, dtype=float32, count=3) self.param1 = nums[0] self.param2 = nums[1] self.param3 = nums[2]

Output the value of float32

Outputs a total of 12 bytes from the float32 value (4 bytes each) 3 for the value contained in the variable.

params = numpy.array([self.param1, self.param2, self.param3], dtype=float32) return params.tostring()

Recommended Posts

Read and write single precision floating point in Python
Read and write JSON files in Python
Read and write NFC tags in python using PaSoRi
Create and read messagepacks in Python
Write O_SYNC file in C and Python
[Python3] Read and write with datetime isoformat with json
How to convert floating point numbers to binary numbers in Python
Write tests in Python to profile and check coverage
Read and write files with Slackbot ~ Bot development with Python ~
Compare read / write speed and capacity of csv, pickle, joblib, parquet in python environment
Show decimal point in Python
Read and write csv file
Write beta distribution in Python
Write python in Rstudio (reticulate)
Read Euler's formula in Python
Read and write a file
Read Namespace-specified XML in Python
Read Outlook emails in Python
Stack and Queue in Python
Write and read a file
Unittest and CI in Python
Read Fortran output in python
Tips for coding short and easy to read in Python
[Python] Use this to read and write wav files [wavio]
Write a binary search in Python
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Read Protocol Buffers data in Python3
Write JSON Schema in Python DSL
Sorting algorithm and implementation in Python
Write an HTTP / 2 server in Python
Write AWS Lambda function in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Read and use Python files from Python
Write A * (A-star) algorithm in Python
Assignments and changes in Python objects
Read big endian binary in Python and convert it to ndarray
Check and move directories in Python
Preserve and read order in PyYAML
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Write selenium test code in python
Hashing data in R and Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Function synthesis and application in Python
Read files in parallel with Python
Export and output files in Python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Ignore # line and read in pandas
Write C unit tests in Python
How to write the correct shebang in Perl, Python and Ruby scripts
Let's throw away JavaScript and write a web front end in Python!
Check the in-memory bytes of a floating point number float in Python
Write documentation in Sphinx with Python Livereload