Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15272936
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类:

2010-01-28 22:43:56

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;

阅读(8505) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~