Delphi hexadecimal conversion

Source: Internet
Author: User

1. hextobin () hexadecimal conversion binary
Unit: Classes
Delphi Syntax:

function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer

Description:
Call the hextobin function to convert the hexadecimal string to the corresponding binary value.
Text is a string that represents the hexadecimal value.
Buffer returns the converted binary result value.
Buffersize indicates the buffer size. Text must point to at least 2 x bufsize hexadecimal characters, because every two hexadecimal characters are expressed as one byte.
Hextobin is returned in the buffer because the text does not contain valid hexadecimal characters ('0'... 'F') and the number of characters has not yet been used.
Note: lowercase characters must be used in hexadecimal notation. hextobind cannot recognize uppercase characters.

2. bintohex () binary conversion hexadecimal
Unit: Classes
Delphi Syntax:

procedure BinToHex(Buffer, Text: PChar; BufSize: Integer);

Description:
Call bintohex to convert the binary value in the buffer to the hexadecimal string it represents.
Buffer is a byte buffer, which contains binary values.
Text returns a string ending with null, indicating the value of buffer as the hexadecimal number.
Bufsize indicates the buffer size. Text must point to a series of characters, which must contain at least 2 * bufsize bytes.

3. inttohex () converts an integer to a hexadecimal number.
Unit: sysutils
Delphi Syntax:


function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;

Description:
Inttohex converts a number to a string in hexadecimal format. Value is the number to be converted. The digits parameter specifies the minimum character width. If the minimum width is insufficient, it is filled with 0.

4. convert a strtoint () string to an integer.
Unit: sysutils
Delphi Syntax:

function StrToInt(const S: string): Integer;

Description:
Returns the string s to an integer. If the string is not expressed as an integer, an exception occurs. to convert a hexadecimal string to an integer, add $ before the string.

5. convert an integer into a binary string.


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. hexadecimal conversion binary


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. octal and decimal conversion


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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.