Skip to content

Instantly share code, notes, and snippets.

@g8up
Last active July 20, 2025 07:08
Show Gist options
  • Save g8up/6579e021e390203e07dda7b6a054a8ea to your computer and use it in GitHub Desktop.
Save g8up/6579e021e390203e07dda7b6a054a8ea to your computer and use it in GitHub Desktop.
把一个 8位 HEX 字符串转换为浮点数
/**
* 解码:把一个 8位 HEX 字符串转换为浮点数
*/
function Hex2Float( hexStr ) {
// 确保字符串长度为偶数
if (hexStr.length % 2 !== 0) {
throw new Error("Hex string length must be even");
}
const buffer = new ArrayBuffer(hexStr.length / 2);
const dv = new DataView(buffer);
dv.setUint32(0, `0x${hexStr}`);
return dv.getFloat32(0);
}
// test
console.log(Hex2Float("BB4E28DE")); // -0.003145746421068907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment