Delphi下常用转换(一)

    技术2022-05-11  127

    Delphi下常用转换作者:lyboy99 e-mail:lyboy99@sina.com   url: http://hnh.126.com给大家提供几个常用的转换方法和其转换函数希望可以对你有帮助

    1.HEX -> Integer2.2.Dec To HEX3.ASCII to HEX / math4.Binary to decimal

    ==============================================================1.HEX -> Integer==============================================================方法. 1 --------------------------------------------------------------------------------

    var  i : integer  s : string;begin  s := '$' + ThatHexString;  i := StrToInt(a);end;

    --------------------------------------------------------------------------------

    方法. 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;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 then     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(var Dst: string; Src: PChar; Len: Integer);  begin    if Len > 255 then      Move(Src^, Dst[1], 255)    else      Move(Src^, Dst[1], Len);    SetLength(Dst, Len);  end;{$ENDIF}end.

    =================================================================4.Binary to decimal-----------------------------------------------------------------

    // 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,100000000            // check upper limit  jb     @1                       // ok  mov    eax,-1                   // error flag  jmp    @exit                    // exit with -1@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                      // eax DIV 10, edx mod 10  add    esi,edx                  // result = result + remainder[I]  ror    esi,4                    // shift nibble  loop @2                       // loop for all 8 nibbles  mov    eax,esi                  // function result  pop    esi                      // restore registers  pop    ebx@exit:end;

    --------------------------------------------------------------------------------

    方法. 2[Oliver Townshend, oliver@zip.com.au]

    --------------------------------------------------------------------------------

    function IntToBin(Value: LongInt;Size: Integer): String;var i: Integer;begin Result:=''; for i:=Size downto 0 do begin    if Value and (1 shl i)<>0 then begin       Result:=Result+'1';  end else begin       Result:=Result+'0';  end; end;end;

    function BinToInt(Value: String): LongInt;var i,Size: Integer;begin Result:=0; Size:=Length(Value); for i:=Size downto 0 do begin    if Copy(Value,i,1)='1' then begin       Result:=Result+(1 shl i);  end; end;end;

    ===========================================================================


    最新回复(0)