1.HexToBin() 十六進位轉換二進位
所在單元:Classes
Delphi文法:
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer |
描述:
調用HexToBin函數轉換十六進位字串到相應的二進位值。
Text是一個表示十六進位值的字串。
Buffer返迴轉換後的二進位結果值。
BufferSize表示Buffer的大小。Text需要指向至少2*BufSize的十六進位字元,因為每兩個十六進位字元表現為一個位元組。
HexToBin返回在Buffer中因為Text沒有包含有效十六進位字元('0'..'f')而還沒有被用的字元數量.
注意:十六進位數必須使用小寫字元;HexToBind不能識別大寫字元。
2.BinToHex() 二進位轉換十六進位
所在單元:Classes
Delphi文法:
procedure BinToHex(Buffer, Text: PChar; BufSize: Integer); |
描述:
調用BinToHex轉換buffer中的二進位值為它所表示的十六進位字串
Buffer是一個位元組的緩衝區,其中包含二進位值
Text返回一個以null為結束字元的字串,表示Buffer作為十六進位數的值
BufSize表示Buffer的大小。Text需要指向一系列字元,這些字元至少有2*BufSize大小位元組。
3.IntToHex()將整型數轉換為十六進位數
所在單元:SysUtils
Delphi文法:
function IntToHex(Value: Integer; Digits: Integer): string; overload; function IntToHex(Value: Int64; Digits: Integer): string; overload; |
描述:
IntToHex轉換一個數字為這個數字十六進位表示的字串。Value是要轉換的數字。參數Digits指定字元最小寬度,最小寬度不足時將用0填充。
4.StrToInt()字串轉換成整型數
所在單元:SysUtils
Delphi文法:
function StrToInt(const S: string): Integer; |
描述:
返回字串S轉換成整數,字串非整數表達時將引起異常,十六進位字串轉換為整型數要求在字串前面添加$即可。
5.把一個整數變成二進位字串
function IntToBinaryStr(TheVal: LongInt): string; var counter: LongInt; begin {This part is here because we remove leading zeros. That means that a zero value would return an empty string.} if TheVal = 0 then begin result := '0'; exit; end; result := ''; counter := $80000000; {Suppress leading zeros} while ((counter and TheVal) = 0) do begin counter := counter shr 1; if (counter = 0) then break; {We found our first "1".} end; while counter > 0 do begin if (counter and TheVal) = 0 then result := result + '0' else result := result + '1'; counter := counter shr 1; end; end;// Binary to Integer function BinToInt(Value: string): Integer; var i, iValueSize: Integer; begin Result := 0; iValueSize := Length(Value); for i := iValueSize downto 1 do if Value[i] = '1' then Result := Result + (1 shl (iValueSize - i)); end; // Integer to Binary function IntToBin(Value: Longint; Digits: Integer): string; var i: Integer; begin Result := ''; for i := Digits downto 0 do if Value and (1 shl i) <> 0 then Result := Result + '1' else Result := Result + '0'; end;
|
6.十六進位轉換二進位
function HexToBin(Hexadecimal: string): string; const BCD: array [0..15] of string = ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111'); var i: integer; begin for i := Length(Hexadecimal) downto 1 do Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result; end; |
7.八進位和十進位的轉換
function OctToInt(Value: string): Longint; var i: Integer; int: Integer; begin int := 0; for i := 1 to Length(Value) do begin int := int * 8 + StrToInt(Copy(Value, i, 1)); end; Result := int; end;function IntToOct(Value: Longint; digits: Integer): string; var rest: Longint; oct: string; i: Integer; begin oct := ''; while Value <> 0 do begin rest := Value mod 8; Value := Value div 8; oct := IntToStr(rest) + oct; end; for i := Length(oct) + 1 to digits do oct := '0' + oct; Result := oct; end;
|