Skip to content

Instantly share code, notes, and snippets.

Avatar

Jože Vovk Jozo132

View GitHub Profile
@Jozo132
Jozo132 / float32encoding.js
Last active Jun 3, 2020
JavaScript (Node.js) IEEE 754 Single precision Floating-Point (32-bit) binary conversion from and to both Hex and Bin
View float32encoding.js
// Forked 'toFloat' from https://gist.github.com/laerciobernardo/498f7ba1c269208799498ea8805d8c30
// Forked 'toHex' from stackoverflow answer https://stackoverflow.com/a/47187116/10522253
// Modifyed by: Jozo132 (https://github.com/Jozo132)
const Float32ToHex = (float32) => {
const getHex = i => ('00' + i.toString(16)).slice(-2);
var view = new DataView(new ArrayBuffer(4))
view.setFloat32(0, float32);
return Array.apply(null, { length: 4 }).map((_, i) => getHex(view.getUint8(i))).join('');
}
You can’t perform that action at this time.