Delphi under the common conversion of: lyboy99 e-mail: lyboy99@sina.com url: http://hnh.126.com for everyone to provide some common conversion method and conversion functions hope it can help you
1.HEX -> Integer2.2.dec to hex3.ascii to hex / math4.binary to decimal
============================================================================================================================================================================================================= ============ 1.h -> integer ================================ ======================================= method. 1 ----------------- -------------------------------------------------- -------------
Var i: integer s: string; begin s: = '$' ThathexString; i: = strt (a); end;
-------------------------------------------------- ------------------------------
Method. 2
-------------------------------------------------- ------------------------------
Const HEX: Array ['a' .. 'f'] of integer = (10, 11, 12, 13, 14, 15); var str: string; int, i: integer; begin readln (STR); int: = 0; for i: = 1 to length (str) do if str [i] <'a' Then Int: = INT * 16 ORD (STR [I]) - 48 else int: = int * 16 hex [ STR [I]]; Writeln (int); readln; end.
============================================================================================================================================================================================================= = 2.Dec to Hex -------------------------------------- HexString: = Format (' % 0x ', DecValue; --------------------------------------
============================================================================================================================================================================================================= == 3.ascii to HEX / MATH ------------------------ Unit HexStr;
InterfaceUses string16, sysutils;
TYPE PBYTE = ^ Byte;
procedure BytesToHexStr (var hHexStr: String; pbyteArray: PByte; InputLength: WORD); procedure HexStrToBytes (hHexStr: String; pbyteArray: Pointer); procedure HexBytesToChar (var Response: String; hexbytes: PChar; InputLength: WORD);
implementationprocedure BytesToHexStr (var hHexStr: String; pbyteArray: PByte; InputLength: WORD); Const HexChars: Array [0..15] of Char = '0123456789ABCDEF'; var i, j: WORD; begin SetLength (hHexStr, (InputLength * 2 )); Fillchar (HHEXSTR, SIZEOF (HHEXSTR), # 0); J: = 1; for i: = 1 to InputLength Do Begin HhexStr [J]: = char (HEXCHARS [PBYTEARRAY ^ SHR 4]); inc (J HHEXSTR [J]: = char (HexChars [pbytearray ^ and 15]); INC (J); Inc (PbyTearRay); end;
procedure HexBytesToChar (var Response: String; hexbytes: PChar; InputLength: WORD); var i: WORD; c: byte; begin SetLength (Response, InputLength); FillChar (Response, SizeOf (Response), # 0); for i: = 0 TO (InputLength - 1) Do Begin C: = Byte (HEXBYTES [I]) And byte ($ F); IF C> 9 INC (C, $ 37) Else Inc (C, $ 30); response [i 1]: = char (c); End; {for} end; procedure hexstrtobytes (hHexStr: string; pbyteaRray: Pointer); {PbyteArray Must Point To Enough Memory To Hold The Output} Var i, J: Word; Tempptr: Pchar Twodigits: String [2]; begin tempptr: = pbyteArray; J: = 1; for i: = 1 to (length (hHexStr) Div 2) Do Begin Twodigits: = Copy (HHEXSTR, J, 2); INC (J , 2); PBYTE (Tempptr) ^: = STRTOINT ('$' Twodigits); INC (Tempptr); end; {for} end;
End.
-------------------------------------------------- ------------------------------
-------------------------------------------------- ------------------------------
UNIT String16.interface {$ IFNDEF Win32} procedure SetLength (var S: string; Len: Integer); procedure SetString (var Dst: string; Src: PChar; Len: Integer); {$ ENDIF} implementation {$ IFNDEF Win32} procedure SETLENGTH (VAR S: String; Len: Integer); Begin If Len> 255 Then S [0]: = CHR (255) Else S [0]: = Chr (LEN) end;
Procedure setstring; src: pchar; len: integer; beginiff (src ^, dst [1], 255) Else Move (src ^, DST [1], LEN); setlength (DST, LEN); End; {$ ENDIF} end.
============================================================================================================================================================================================================= ================================================================================================================================================ ----------------------------------- // Convert 32 bit base2 to 32 bit base10 ///max Number = 99 999 999, return -1 if more //
Function base10 (Base2: Integer): Integer; Assembler; ASM CMP Eax, 1000000 // Check Upper Limit JB @ 1 // OK MOV EAX, -1 // Error Flag JMP @ 1: Push EBX // Save Registers Push ESI XOR ESI, ESI // Result = 0
MOV EBX, 10 // Diveder Base 10 MOV ECX, 8 // 8 Nibbles (10 ^ 8-1) @ 2: Mov Edx, 0 // Clear RemainDer Div EBX // ESI, EDX MOD 10 Add ESI, EDX // Result = result remainder [i] ROR ESI, 4 // Shift Nibble Loop @ 2 // Loop for ALL 8 NIBBLES MOV ESI // RESI // Function Result Pop ESI // RESTORE Registers Pop EBX @ EXIT: END;
-------------------------------------------------- ------------------------------
Method. 2 [oliver townshend, oliver@zip.com.au]
-------------------------------------------------- ------------------------------
Function INTTOBIN (SIZE: INTEGER): String; var i: integer; begin result: = '; for i: = size downto 0 do begin if value and (1 shli i) <> 0 THEN Begin Result: = Result '1'; END ELSE BEGIN RESULT: = Result '0'; end; end; end; function bintoint: longint; var i, size: integer; begin result: = 0; size: = length: = length (Value); for i: = size downto 0 do begin if copy (value, i, 1) = '1' Then Begin Result: = Result (1 shli i); end; end;
============================================================================================================================================================================================================= =========================