Arrays PowerShell:将十六进制变量转换为单个(float32)big-endian变量
如何将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
'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)
- 请注意,传递给