将 32 位无符号“实数”数据类型(分为两个 16 位有符号字)转换为 javascript

roo*_*t66 5 javascript floating-point node.js

我有一个 32 位无符号“实数”值,分成两个 16 位有符号“字”(0-65535) 值。如何将它们转换为 JavaScript 数字?

示例:值 1:18584 值 2:18081 实际值为:20644.3

我正在寻找像 back2Real(18584, 18081) 这样返回 20644.3 的函数。这些值来自 modbus 应用程序 (NodeJS / modbus_stack)。SPS/modbus 服务器发送一个分为两个字寄存器的“实数”值。

问候,root66

dan*_*uzz 5

您可以使用新的类型化数组功能来简化此过程。

function uint16ToFloat32(low, high) {
  var buffer = new ArrayBuffer(4);
  var intView = new Uint16Array(buffer);
  var floatView = new Float32Array(buffer);

  intView[0] = low;
  intView[1] = high;
  return floatView[0];
}

function float32ToUint16(value) {
  var buffer = new ArrayBuffer(4);
  var intView = new Uint16Array(buffer);
  var floatView = new Float32Array(buffer);

  floatView[0] = value;
  return [intView[0], intView[1]];
}

console.log("Converted ints to", uint16ToFloat32(18584, 18081));
console.log("Converted float to", float32ToUint16(20644.297));
Run Code Online (Sandbox Code Playgroud)

这是一个文字记录:

$ node floatsplit.js
Converted ints to 20644.296875
Converted float to [ 18584, 18081 ]
$
Run Code Online (Sandbox Code Playgroud)


Pet*_* O. 2

使用此函数转换为 JavaScript 数字。由于 JavaScript 使用双精度而不是单精度数字,因此可能会发生一些舍入。

function back2Real(low, high){
  var fpnum=low|(high<<16)
  var negative=(fpnum>>31)&1;
  var exponent=(fpnum>>23)&0xFF
  var mantissa=(fpnum&0x7FFFFF)
  if(exponent==255){
   if(mantissa!=0)return Number.NaN;
   return (negative) ? Number.NEGATIVE_INFINITY :
         Number.POSITIVE_INFINITY;
  }
  if(exponent==0)exponent++;
  else mantissa|=0x800000;
  exponent-=127
  var ret=(mantissa*1.0/0x800000)*Math.pow(2,exponent)
  if(negative)ret=-ret;
  return ret;
}
Run Code Online (Sandbox Code Playgroud)

以下函数将 JavaScript 数字转换为 32 位 IEEE 浮点数,分为低位字和高位字:

function real2Back(value){
  if(isNaN(value))return [0,0xFFC0]
  if(value==Number.POSITIVE_INFINITY || value>=3.402824e38)
    return [0,0x7F80]
  if(value==Number.NEGATIVE_INFINITY || value<=-3.402824e38)
    return [0,0xFF80]
  var negative=(value<0)
  var p,x,mantissa
  value=Math.abs(value)
  if(value==2.0)return [0,0x4000]
  else if(value>2.0){
   // positive exponent
   for(var i=128;i<255;i++){
     p=Math.pow(2,i+1-127)
     if(value<p){
      x=Math.pow(2,i-127)
      mantissa=Math.round((value*1.0/x)*8388608)
      mantissa&=0x7FFFFF
      value=mantissa|(i<<23)
      if(negative)value|=(1<<31)
      return [value&0xFFFF,(value>>16)&0xFFFF]
     }
   }
   // return infinity
   return negative ? [0,0xFF80] : [0,0x7F80]
  } else {
   for(var i=127;i>0;i--){
   // negative exponent
     p=Math.pow(2,i-127)
     if(value>p){
      x=p
      mantissa=Math.round(value*8388608.0/x)
      mantissa&=0x7FFFFF
      value=mantissa|(i<<23)
      if(negative)value|=(1<<31)
      return [value&0xFFFF,(value>>16)&0xFFFF]
     }
   }
   // subnormal
   x=Math.pow(2,i-126)
   mantissa=Math.round((value*8388608.0/x))
   if(mantissa>0x7FFFFF)mantissa=0x800000
   value=mantissa
   if(negative)value|=(1<<31)
   return [value&0xFFFF,(value>>16)&0xFFFF]   
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。该代码属于公共领域。