哈哈,已经搞定了:)
var
BetaTable4: array [0..Ord('f') * 256 + 255] of Word;
procedure InitBetaTable4;
const
ValidChars = ['0'..'9', 'A'..'F', 'a'..'f'];
var
i, j: Integer;
begin
FillChar(BetaTable4[0], SizeOf(BetaTable4), $FF);
for i := 0 to Ord('f') do
begin
if Chr(i) in ValidChars then
for j := 0 to Ord('f') do
begin
if Chr(j) in ValidChars then
BetaTable4[j * 256 + i] := StrToInt('$' + Chr(i) + Chr(j));
end;
end;
end;
function HexToInt_beta4(const S: string): DWORD;
const
SmallTbl: array [0..255] of Byte = (
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 16, 16, 16, 16, 16, 16,
16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16);
asm
push ebx
push ecx
push edx
push esi
push edi
mov edi, esp
push 0
mov esi, eax //字符串地址
mov ecx, [eax - 4] //读取字符串长度
test esi, esi //判断是否为空指针
jz @Err
test ecx, ecx //判断字符串是否为空
jle @Err
lea eax, BetaTable4
xor edx, edx
xor ebx, ebx
test ecx, 1
jz @LeftBytes
//FirstByte:
lea eax, SmallTbl
mov bl, [esi]
mov bl, [eax][ebx]
test ebx, 16
jnz @Err
dec edi
mov [edi], bl
inc esi
lea eax, BetaTable4
dec ecx
jz @Ext
@LeftBytes:
mov bx, [esi]
cmp bl, 'f'
ja @Err
mov dx, [eax][ebx * 2]
test dh, dh
jnz @Err
dec edi
mov [edi], dl
inc esi
inc esi
dec ecx
dec ecx
jnz @LeftBytes
@Ext:
pop eax
pop edi
pop esi
pop edx
pop ecx
pop ebx
ret
@Err:
mov [esp], 0
pop eax
pop edi
pop esi
pop edx
pop ecx
pop ebx
end;
测试结果如下:
HexToInt_tseug0 (170ms): 2882343476.
HexToInt_tseug2 (140ms): 2882343476.
HexToInt_DoubleWood1(150ms): 2882343476.
HexToInt_DoubleWood2(151ms): 2882343476.
HexToInt_beta4 (120ms): 2882343476.
HexToInt_tseug0 (171ms): 2882343476.
HexToInt_tseug2 (130ms): 2882343476.
HexToInt_DoubleWood1(160ms): 2882343476.
HexToInt_DoubleWood2(150ms): 2882343476.
HexToInt_beta4 (100ms): 2882343476.
注:我前面的 beta2, beta3 处理奇数长度字符串时有问题,没时间改了,
反正这个 beta4 很好用,而且不出错,就行了:)