国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Delphi > 正文

Delphi下常用轉(zhuǎn)換(一)

2019-11-18 18:47:48
字體:
供稿:網(wǎng)友

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

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常見圖象格式轉(zhuǎn)換技術(shù)(二)

下一篇:Delphi常見圖象格式轉(zhuǎn)換技術(shù)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點(diǎn)

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 米泉市| 沈丘县| 萨迦县| 连南| 兴国县| 遵义县| 沙坪坝区| 琼结县| 灵川县| 六枝特区| 竹北市| 邵武市| 呼伦贝尔市| 菏泽市| 宝鸡市| 青川县| 临夏市| 库尔勒市| 佛教| 罗定市| 新野县| 萍乡市| 乌兰察布市| 宁夏| 黄石市| 桑日县| 正定县| 固镇县| 贡觉县| 麻阳| 宁晋县| 达尔| 正镶白旗| 建宁县| 肥城市| 寻乌县| 黔江区| 深泽县| 阿荣旗| 肥西县| 长白|