Как преобразовать шестнадцатеричную переменную с прямым порядком байтов = '0x41fe89de' в переменную [single] (float32)?
'0x41fe89de'
[single]
Результат должен быть 31,8173179626465.
31,8173179626465
Я знаю только путь с [double] "0x41fe89de", получая результат 1107200478.
[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 (с форматированием вывода по умолчанию).
31.81732
Техника, используемая для преобразования шестнадцатеричной строки в байтовый массив, описана в этот ответ .
Преобразование байтового массива в собственный экземпляр [single] платформы (System.Single) выполняется через System.BitConverter class.
System.Single
System.BitConverter
::ToSingle()
4
32
8
0
[System.Runtime.InteropServices.Marshal]::SizeOf([single] 0)