Code
function Strtohex (Const str:ansistring): ansistring;
Asm
Push EBX
Push ESI
Push EDI
Test Eax,eax
JZ @ @Exit
MOV Esi,edx//Save the EdX value, the address used to generate the new string
MOV edi,eax//Save original string
MOV edx,[eax-4]//Get string length
Test Edx,edx//Check length
Je @ @Exit {Length (S) = 0}
MOV Ecx,edx//Save length
Push ECX
SHL edx,1
MOV Eax,esi
{$IFDEF VER210}
movzx ecx, Word ptr [edi-12] {need to set codepage}
{$ENDIF}
call [email protected]//set new string length
MOV Eax,esi//new string address
Call Uniquestring//Generate a unique new string, string position in EAX
Pop ECX
@ @SetHex:
XOR Edx,edx//emptying edx
mov dl, [edi]//str string character
MOV Ebx,edx//Save the current character
SHR edx,4//Right Shift 4 bytes, get high 8 bits
mov dl,byte ptr[[email protected] @HexChar]//Convert to Characters
mov [EAX],DL//String input into the new string to be stored
and ebx,$0f//Get low 8 bit
mov dl,byte ptr[[email protected] @HexChar]//Convert to Characters
inc EAX//Move one byte, store low
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//is an empty string
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//start construction string
{$IFDEF VER210}
movzx ecx, Word ptr [edi-12] {need to set codepage}
{$ENDIF}
call [email protected]//set new string length
MOV Eax,esi//new string address
Call Uniquestring//Generate a unique new string, string position in EAX
Pop ECX
XOR EBX,EBX
XOR Esi,esi
@ @CharFromHex:
XOR Edx,edx
mov dl, [edi]//str string character
CMP dl, ' 0 '//See if the character is between 0 and F
JB @ @Exit//less than 0, exit
CMP DL, ' 9 '//Less than =9
JA @ @DoChar//compoknum
Sub dl, ' 0 '
JMP @ @DoConvert
@ @DoChar:
First turn into uppercase characters
and DL, $DF
CMP dl, ' F '
JA @ @Exit//greater than F exit
Add dl,10
Sub dl, ' A '
@ @DoConvert://Conversion
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: = ' no idle time ';
str: = Strtohex (str);
ShowMessage (str);
ShowMessage (Hextostr (str));
End
The assembly code of the Delphi string with the 16-binary reciprocal conversion function