a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Delphi > 正文

Delphi下常用轉換(一)

2019-11-18 18:47:48
字體:
來源:轉載
供稿:網友

Delphi下常用轉換
作者:lyboy99
e-mail:lyboy99@sina.com  
url: http://hnh.126.com
給大家提供幾個常用的轉換方法和其轉換函數
希望可以對你有幫助

1.HEX -> Integer
2.2.Dec To HEX
3.ASCII to HEX / math
4.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;

interface
uses 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);

implementation
procedure 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;


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


上一篇:Delphi常見圖象格式轉換技術(二)

下一篇:Delphi常見圖象格式轉換技術

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 丝袜 亚洲 另类 欧美 综合 | 国产一区免费在线观看 | 亚洲高清视频在线观看 | 男人久久天堂 | 国产成人免费网站 | 国产精品视频在线观看 | 午夜视频在线 | 久久品 | 亚洲国产一区二区三区在线观看 | 色999精品 | 欧洲毛片 | www污在线观看 | 亚洲一级黄色 | 国产成人高清视频 | 一区二区三区 在线 | 亚洲一二三区在线观看 | 天天做天天爱天天综合网2021 | 小罗莉极品一线天在线 | 亚洲精品日韩激情欧美 | 毛片网子 | 国产成人中文字幕 | 日日摸日日碰夜夜爽不卡dvd | 国产精品一区二区在线 | 999热在线 | 欧美三级在线 | 狠狠色狠狠色综合网 | 国产日韩精品入口 | 亚洲小视频网站 | 黄色成人av| 国产精品中文字幕一区二区三区 | 国产视频一区二区在线观看 | 亚洲码欧美码一区二区三区 | xxx在线观看 | 免费看男女www网站入口在线 | 日韩中文在线视频 | 国产精品乱码一区二区三区 | 亚洲风情在线观看 | 91精品国产aⅴ | 欧美精品一区二区三区免费视频 | 欧美一区精品 | 超碰日本 |