function StrToHex(Const str: Ansistring): Ansistring; asm push
ebx push esi push edi test eax,eax jz @@Exit mov esi,edx
//保存edx值,用来产生新字符串的地址 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
//产生一个唯一的新字符串,串位置在eax中 Pop ecx @@SetHex: xor edx,edx //清空edx mov dl,
[edi] //Str字符串字符 mov ebx,edx //保存当前的字符 shr edx,4 //右移4字节,得到高8位 mov
dl,byte ptr[edx+@@HexChar] //转换成字符 mov [eax],dl //将字符串输入到新建串中存放 and
ebx,$0F //获得低8位 mov dl,byte ptr[ebx+@@HexChar] //转换成字符 inc eax
//移动一个字节,存放低位 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 //产生一个唯一的新字符串,串位置在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: //先转成大写字符 and dl,$DF cmp dl,'F' ja @@Exit
//大于F退出 add dl,10 sub dl,'A' @@DoConvert: //转化 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;
|