年终奖没发,心情有点低落,不想做公司的东西,于是将以前一个朋友问的关于字符串与16进制串的相互转换的汇编代码实现了一下,没啥技术含量,按照Delphi的汇编规则走就行了,里面也没有涉及到任何算法!速度快或 不快都不重要,仅仅是无事发泄一下的玩意(只测试2007,2010的我没测试,如果有2010的自行测试一下),代码如下:
代码
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 ;
procedure TForm1.Button1Click(Sender: TObject);
var
str: Ansistring;
begin
str : = ' 不得闲按时打发 ' ;
str : = StrToHex(str);
ShowMessage(str);
ShowMessage(HexToStr(str));
end ;
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 ;
procedure TForm1.Button1Click(Sender: TObject);
var
str: Ansistring;
begin
str : = ' 不得闲按时打发 ' ;
str : = StrToHex(str);
ShowMessage(str);
ShowMessage(HexToStr(str));
end ;
本文转自 不得闲 博客园博客,原文链接:
http://www.cnblogs.com/DxSoft/archive/2010/02/06/1664930.html
,如需转载请自行联系原作者