16进制转10进制算法(已解决)
本帖最后由 fuldho 于 2019-12-30 11:35 编辑最近看磁盘分区表,关于16进制转10进制计算,(36 C1 C0 08)表示当前分区大小,应为低位在前,交换为0x08C0C136 ,
高地位交换再转换为10进制,各位大仙有较为简单算法。
Global $hData = '36C1C008'
Global $iEx, $iIntData, $fData
Global $iDec
For $i = StringLen($hData) - 1 To 1 Step -2
$fData &= StringMid($hData, $i, 2)
Next
$hData = _NumberToBinary('0x' & $fData)
ConsoleWrite($hData & @CRLF)
$iEx = _BinaryToDec(StringMid($hData, 2, 8)) - 127
For $i = 23 To $iEx
$hData &= '0'
Next
$iIntData = '1' & StringMid($hData, 10, $iEx)
$iDec = _BinaryToDec($iIntData) + switchbinary(StringMid($hData, $iEx + 10))
MsgBox(0, 0, $iDec)
Func switchbinary($data)
Local $iRet = 0
Local $Len = StringLen($data)
For $i = $Len To 1 Step -1
$iS = Number(StringMid($data, $i, 1)) * 2 ^ (-1 * $i)
$iRet += $iS
Next
Return StringFormat('%.02f', $iRet)
EndFunc ;==>switchbinary
Func _NumberToBinary($iNumber)
Local $iTopBit, $sBinString = ""
If $iNumber < -2147483648 Or $iNumber > 4294967295 Then Return SetError(1, 0, "")
If $iNumber > 2147483647 Or $iNumber < 0 Then
$iTopBit = 1
Else
$iTopBit = 0
EndIf
Local $iUnsignedNumber = BitAND($iNumber, 0x7FFFFFFF)
Do
$sBinString = BitAND($iUnsignedNumber, 1) & $sBinString
$iUnsignedNumber = BitShift($iUnsignedNumber, 1)
Until Not $iUnsignedNumber
Return $iTopBit & StringRight("000000000000000000000000000000" & $sBinString, 31)
EndFunc ;==>_NumberToBinary
Func _BinaryToDec($sBinStr)
Local $sDecStr = 0
If Not StringRegExp($sBinStr, "^{1,63}$") Then Return SetError(1, 0, "")
For $i = 1 To StringLen($sBinStr)
$sDecStr = $sDecStr * 2 + StringMid($sBinStr, $i, 1)
Next
Return $sDecStr
EndFunc ;==>_BinaryToDec
Local $sStr = '36 C1 C0 08'
$sStr = StringRegExpReplace($sStr,'\W','')
Local $sRet = StringRegExpReplace($sStr,'(..)(..)(..)(..)','0x${4}${3}${2}${1}')
MsgBox(0,$sRet,Dec(Hex($sRet)))
非常的给力,再次谢谢
页:
[1]