简体   繁体   English

如何将两个16位整数(高位字/低位字)转换为32位浮点数?

[英]How To Convert Two 16bit Integer (High Word / Low Word) into 32bit Float?

I have two 16bit integer raw data. 我有两个16位整数原始数据。

For example: 例如:
High Word = 17142 (dec) or 0100001011110110 (binary) 高位字= 17142(十进制)或0100001011110110(二进制)
Low Word = 59759 (dec) or 1110100101111001 (binary) 低位字= 59759(十进制)或1110100101111001(二进制)

If you treat two word together as one 32bit float data, it will be "123.456" 如果将两个字一起视为一个32位浮点数据,则将为“ 123.456”
Binary --> 01000010111101101110100101111001 二进制-> 01000010111101101110100101111001

How to convert integer array [59759 , 17142] to float 123.456 in Javascript? 如何在Javascript中将整数数组[59759,17142]转换为浮点型123.456?

Note: [ X (16bit low word) , Y (16 bit high word) ] ==> Z (32bit float) 注意:[X(16位低位字),Y(16位高位字)] ==> Z(32位浮点型)

You can do this with typed arrays and ArrayBuffer , which allow you to interpret the same bits in different ways (but endianness is platform-specific). 您可以使用类型化数组ArrayBuffer来执行此操作,这可以使您以不同的方式解释相同的位(但字节序特定于平台)。 It's also possible using a DataView on the buffer, which lets you conrol endianness. 也可以在缓冲区上使用DataView ,这可以控制字节顺序。

Here's the typed array approach which works with the endianness of my platform, see comments: 这是与我的平台的字节序配合使用的类型化数组方法,请参见注释:

 // Create a buffer var buf = new ArrayBuffer(4); // Create a 16-bit int view of it var ints = new Uint16Array(buf); // Fill in the values ints[0] = 59759; ints[1] = 17142; // Create a 32-bit float view of it var floats = new Float32Array(buf); // Read the bits as a float; note that by doing this, we're implicitly // converting it from a 32-bit float into JavaScript's native 64-bit double var num = floats[0]; // Done console.log(num); 

Here's the DataView approach, note writing the ints in the opposite order: 这是DataView方法,请注意以相反的顺序编写整数:

 // Create a buffer var buf = new ArrayBuffer(4); // Create a data view of it var view = new DataView(buf); // Write the ints to it view.setUint16(0, 17142); view.setUint16(2, 59759); // Read the bits as a float; note that by doing this, we're implicitly // converting it from a 32-bit float into JavaScript's native 64-bit double var num = view.getFloat32(0); // Done console.log(num); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 32 位整数散列到 16 位整数? - Hash 32bit int to 16bit int? javascript sharedArrayBuffer 和按位运算返回 32 位而不是 16 位数字 - javascript sharedArrayBuffer and bitwise operations returning a 32bit instead of 16bit number 将32位无符号“实数”数据类型(分成两个16位有符号字)转换为javascript - Convert 32bit unsigned “Real” data type (splitted into two 16 bit signed words) to javascript 如何将64位整数分割为两个32位整数 - how to split 64bit integer to two 32bit integers 在JavaScript中将两个16位数组合并转换为一个32位数(浮点数) - Combine and convert two 16 bit numbers into one 32 bit number (float) in JavaScript 在JavaScript中将float转换为32位十六进制字符串 - Convert float to 32bit hex string in JavaScript 在javascript中将32位整数转换为4字节的数据 - Convert a 32bit integer into 4 bytes of data in javascript 如何在javascript中将双精度(64位)数转换/存储到32位浮点数或UInt16 - How to convert/store a double precision (64bit) number to a 32 bit float or a UInt16 in javascript Javascript问题:如何将十六进制数转换为高和低32位值 - Javascript Question: How to Convert Hexadecimal Number to High and Low 32-bit values javascript 32位整数乘法模拟 - javascript 32bit integer multiply simulation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM