Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PowerShell:将十六进制变量转换为单个(float32)big-endian变量_Arrays_Powershell_Hex_Endianness - Fatal编程技术网

Arrays PowerShell:将十六进制变量转换为单个(float32)big-endian变量

Arrays PowerShell:将十六进制变量转换为单个(float32)big-endian变量,arrays,powershell,hex,endianness,Arrays,Powershell,Hex,Endianness,如何将big-endian十六进制变量='0x41fe89de'转换为[单个](float32)变量 结果必须是318173179626465 我只知道如何使用[double]“0x41fe89de”,得到结果1107200478 # Input: a string representing a number or big-endian byte sequence # in hexadecimal form. $hex = '0x41fe89de' # Convert the hex inpu

如何将big-endian十六进制变量=
'0x41fe89de'
转换为
[单个]
(float32)变量

结果必须是
318173179626465

我只知道如何使用
[double]“0x41fe89de”
,得到结果
1107200478

# Input: a string representing a number or big-endian byte sequence
# in hexadecimal form.
$hex = '0x41fe89de'

# Convert the hex input string to a byte array.
$byteArray = [byte[]] ($hex -replace '^0x' -split '(..)' -ne '' -replace '^', '0x')

# Convert from big-endian to little-endian, if necessary, by
# reversing the order of the bytes to match the platform's.
if ([BitConverter]::IsLittleEndian) {
  [Array]::Reverse($byteArray)
}

# Convert to a platform-native [single] instance.
[BitConverter]::ToSingle($byteArray, 0)
以上结果产生
31.81732
(默认输出格式)

  • 中介绍了将十六进制字符串转换为字节数组的技术

  • 字节数组到平台本机
    [single]
    实例()的转换是通过类执行的

    • 请注意,传递给
      ::ToSingle()
      的字节数必须与构成目标类型的字节数完全匹配;在这种情况下,需要
      4
      字节,因为
      [单个]
      32
      位类型(
      4
      字节乘以
      8
      位);如有必要和适当,提供填充有
      0
      字节的数组;使用诸如
      [System.Runtime.InteropServices.Marshal]:SizeOf([single]0)
      之类的表达式来确定所需的字节计数

十六进制变量来自哪里?snmp代码在这里。我基本上是手工将endian字节转换成整数。