Decode / encode IEEE 754 float value from raw data using Erlang?
New to Erlang here ... I need to extract the IEEE 754 float value from the raw data into a list. For example. Decoding: [42,91,0,0] should equal 72.5 and also convert float to list Encoding: 72.5 should be converted to [42,91,0,0] Are there libraries that support these operations? What's the best practice? Thank you in advance.
+3
source to share
1 answer
For decoding, you can convert the list to binary and then extract the float from the binary (note that the original values ββin your question are hexadecimal, so the list below is prefixed 16#
).
1> <<V:32/float>> = list_to_binary([16#42, 16#91, 0, 0]).
<<66,145,0,0>>
2> V.
72.5
To encode, do the opposite: insert the float value into binary and then convert it to a list:
3> binary_to_list(<<V:32/float>>).
[66,145,0,0]
+7
source to share