function StrToHex(Const str: Ansistring): Ansistring; asm push
ebx push esi push edi test eax,eax jz @@Exit mov esi,edx
//保存edx值,用來產(chǎn)生新字符串的地址 mov edi,eax //保存原字符串 mov edx,[eax-4]
//獲得字符串長度 test edx,edx //檢查長度 je @@Exit {Length(S) = 0} mov ecx,edx
//保存長度 Push ecx shl edx,1 mov eax,esi {$IFDEF VER210} movzx ecx,
word ptr [edi-12] {需要設置CodePage} {$ENDIF} call System.@LStrSetLength
//設置新串長度 mov eax,esi //新字符串地址 Call UniqueString
//產(chǎn)生一個唯一的新字符串,串位置在eax中 Pop ecx @@SetHex: xor edx,edx //清空edx mov dl,
[edi] //Str字符串字符 mov ebx,edx //保存當前的字符 shr edx,4 //右移4字節(jié),得到高8位 mov
dl,byte ptr[edx+@@HexChar] //轉(zhuǎn)換成字符 mov [eax],dl //將字符串輸入到新建串中存放 and
ebx,$0F //獲得低8位 mov dl,byte ptr[ebx+@@HexChar] //轉(zhuǎn)換成字符 inc eax
//移動一個字節(jié),存放低位 mov [eax],dl inc edi inc eax loop
@@SetHex @@Exit: pop edi pop esi pop ebx ret @@HexChar: db
'0123456789ABCDEF' end;
function HexToStr(const Str: AnsiString):
AnsiString; asm push ebx push edi push esi test eax,eax
//為空串 jz @@Exit mov edi,eax mov esi,edx mov edx,[eax-4] test
edx,edx je @@Exit mov ecx,edx push ecx shr edx,1 mov eax,esi
//開始構造字符串 {$IFDEF VER210} movzx ecx, word ptr [edi-12]
{需要設置CodePage} {$ENDIF} call System.@LStrSetLength //設置新串長度 mov eax,esi
//新字符串地址 Call UniqueString //產(chǎn)生一個唯一的新字符串,串位置在eax中 Pop ecx xor
ebx,ebx xor esi,esi @@CharFromHex: xor edx,edx mov dl, [edi]
//Str字符串字符 cmp dl, '0' //查看是否在0到f之間的字符 JB @@Exit //小于0,退出 cmp dl,'9'
//小于=9 ja @@DoChar//CompOkNum sub dl,'0' jmp
@@DoConvert @@DoChar: //先轉(zhuǎn)成大寫字符 and dl,$DF cmp dl,'F' ja @@Exit
//大于F退出 add dl,10 sub dl,'A' @@DoConvert: //轉(zhuǎn)化 inc ebx cmp
ebx,2 je @@Num1 xor esi,esi shl edx,4 mov esi,edx jmp
@@Num2 @@Num1: add esi,edx mov edx,esi mov [eax],dl xor
ebx,ebx inc eax @@Num2: dec ecx inc edi test ecx,ecx jnz
@@CharFromHex @@Exit: pop esi pop edi pop ebx end;
|