如何十六进制转二进制
现有0x004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00要转换成
1000111110这样的形式
我用Binary函数返回值还是16进制的:face (13):
$b1 = StringToBinary("一句话:不看帮助的人,学不会编程.看<<XX编程N日通>的,没多少人能成为高手.",2)
MsgBox(4096, "representation" , $b1)
[ 本帖最后由 kakinkgb 于 2008-8-31 22:16 编辑 ] 一个位的hex能转成4个位的bin,按照这个思路去写udf吧。
PS:谁愿意看一大堆1010满屏飞啊,基本上都用hex代表了,这样看起来要舒服一点…… 你是要转换成字符串形式
udf里面有
谁有高效的算法给我学习一下
udf里面有吗?没看见啊自己做了一个,感觉效率不高,数学苦手能做到这程度已经知足了
dim $bin
$hex='0x004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00'
$hex=StringTrimLeft($hex,2);去掉0x
for $i=1 to StringLen($hex)
$str=StringMid($hex,$i,1)
switch $str
case '0'
$bin=$bin&'0'
case '1'
$bin=$bin&'1'
case '2'
$bin=$bin&'10'
case '3'
$bin=$bin&'11'
case '4'
$bin=$bin&'100'
case '5'
$bin=$bin&'101'
case '6'
$bin=$bin&'110'
case '7'
$bin=$bin&'111'
case '8'
$bin=$bin&'1000'
case '9'
$bin=$bin&'1001'
case 'a'
$bin=$bin&'1010'
case 'b'
$bin=$bin&'1011'
case 'c'
$bin=$bin&'1100'
case 'd'
$bin=$bin&'1101'
case 'e'
$bin=$bin&'1110'
case 'f'
$bin=$bin&'1111'
endswitch
next
MsgBox(0,'', $bin)
忘了加分隔符,汗
[ 本帖最后由 kakinkgb 于 2008-8-31 20:55 编辑 ] 0x0 > 0000
- -四位,不要偷懒 而且你这样做效率太烂了……直接stringreplace,0换0000,1换0001,顶多0~F = 16次调用 楼上好办法! 谢谢分享!!
正找这嘞 验证了一下以下几个方案的效率问题
结果耗时【方案一】 < 【方案二A】 < 【方案二B】
大家觉得各有什么优缺点?
【方案一】$hex='0x004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00'
$hex=StringTrimLeft($hex,2);去掉0x
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
; 初始化结果
$sBinResult = ""
$sBinResult = StringReplace($hex, "0", "0000")
$sBinResult = StringReplace($sBinResult, "1", "0001")
$sBinResult = StringReplace($sBinResult, "2", "0010")
$sBinResult = StringReplace($sBinResult, "3", "0011")
$sBinResult = StringReplace($sBinResult, "4", "0100")
$sBinResult = StringReplace($sBinResult, "5", "0101")
$sBinResult = StringReplace($sBinResult, "6", "0110")
$sBinResult = StringReplace($sBinResult, "7", "0111")
$sBinResult = StringReplace($sBinResult, "8", "1000")
$sBinResult = StringReplace($sBinResult, "9", "1001")
$sBinResult = StringReplace($sBinResult, "A", "1010")
$sBinResult = StringReplace($sBinResult, "B", "1011")
$sBinResult = StringReplace($sBinResult, "C", "1100")
$sBinResult = StringReplace($sBinResult, "D", "1101")
$sBinResult = StringReplace($sBinResult, "E", "1110")
$sBinResult = StringReplace($sBinResult, "F", "1111")
; 打印结果
;ConsoleWrite("Result is " & $sBinResult)
;MsgBox(0, "Result is", $sBinResult)【方案二A】#include <Array.au3>
$hex='0x004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00'
$hex=StringTrimLeft($hex,2);去掉0x
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
; 初始化结果
$sBinResult = ""
; 转换为数组,每一位Hex为数组的一个元素
$aTemp = StringSplit($hex, "")
_ArrayDelete($aTemp, 0)
; 开始转换
For $sHex In $aTemp
Switch $sHex
Case "0"
$sBin = "0000"
Case "1"
$sBin = "0001"
Case "2"
$sBin = "0010"
Case "3"
$sBin = "0011"
Case "4"
$sBin = "0100"
Case "5"
$sBin = "0101"
Case "6"
$sBin = "0110"
Case "7"
$sBin = "0111"
Case "8"
$sBin = "1000"
Case "9"
$sBin = "1001"
Case "A"
$sBin = "1010"
Case "B"
$sBin = "1011"
Case "C"
$sBin = "1100"
Case "D"
$sBin = "1101"
Case "E"
$sBin = "1110"
Case "F"
$sBin = "1111"
Case Else
;
EndSwitch
$sBinResult = $sBinResult & $sBin
Next
; 打印结果
;ConsoleWrite("Result is " & $sBinResult)
;MsgBox(0, "Result is", $sBinResult)【方案二B】#include <Array.au3>
$hex='0x004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00'
$hex=StringTrimLeft($hex,2);去掉0x
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
$hex = $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex & $hex
; 初始化结果
$sBinResult = ""
; 转换为数组,每一位Hex为数组的一个元素
$aTemp = StringSplit($hex, "")
_ArrayDelete($aTemp, 0)
; 开始转换
For $sHex In $aTemp
$sBinResult = $sBinResult & _Hex1ToBin($sHex)
Next
; 打印结果
;ConsoleWrite("Result is " & $sBinResult)
;MsgBox(0, "Result is", $sBinResult)
Func _Hex1ToBin($sHex)
Switch $sHex
Case "0"
$sBin = "0000"
Case "1"
$sBin = "0001"
Case "2"
$sBin = "0010"
Case "3"
$sBin = "0011"
Case "4"
$sBin = "0100"
Case "5"
$sBin = "0101"
Case "6"
$sBin = "0110"
Case "7"
$sBin = "0111"
Case "8"
$sBin = "1000"
Case "9"
$sBin = "1001"
Case "A"
$sBin = "1010"
Case "B"
$sBin = "1011"
Case "C"
$sBin = "1100"
Case "D"
$sBin = "1101"
Case "E"
$sBin = "1110"
Case "F"
$sBin = "1111"
Case Else
;
EndSwitch
Return $sBin
EndFunc 本帖最后由 netegg 于 2014-10-7 14:52 编辑
#include<math.au3>
Local $str = '004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00'
Local $ret = ''
While StringLen($str) > 0
$string = StringLeft($str, 1)
$ret &= StringFormat('%04d', _IntToBin(_hextoint($string))) & ' '
$str = StringTrimLeft($str, 1)
WEnd
MsgBox(0,0,$ret) 首先用到数组就好不了,条件语句case太多,浪费很多时间 还是第一种方法快,不过写起来稍微麻烦点 时间貌似比较短了吧
Local $time = TimerInit()
Local $sout, $p, $temp
Local $sHex = "004EE553DD8B3A000D4E0B772E5EA9528476BA4E2C00665B0D4E1A4F167F0B7A2E000B773C003C0058005800167F0B7A4E00E5651A903E0084762C00A16C1A59115CBA4EFD8010623A4ED89A4B622E00"
Local $mask = 0x8
While StringLen($sHex) > 0
$p = $mask
$temp = StringLeft($sHex, 1)
Switch $temp
Case "A"
$temp = 10
Case "B"
$temp = 11
Case "C"
$temp = 12
Case "D"
$temp = 13
Case "E"
$temp = 14
Case "F"
$temp = 15
EndSwitch
For $i = 0 To 3
$sout &= BitAND($temp, $p) ? 1 : 0
$p = BitShift($p, 1)
Next
$sout &= " "
$sHex = StringTrimLeft($sHex, 1)
WEnd
MsgBox(0, TimerDiff($time), $sout)
页:
[1]