delphi 進位轉換

來源:互聯網
上載者:User
 

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.