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;
//----------------------------------网摘start------------------------------------
//十进制 to 二进制
function IntToBin(Value: LongInt;Size: Integer): String;
var
i: Integer;
begin
Result:='';
for i:=Size-1 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;
//二进制 to 十进制
function BintoInt(Value: String): LongInt;
var
i,Size: Integer;
begin
Result:=0;
Size:=Length(Value);
for i:=Size downto 1 do
begin
if Copy(Value,i,1)='1' then
Result:=Result+(1 shl (Size-i));
end;
end;
function floatBintoInt(Value: String): real;
var
i,Size: Integer;
begin
Result:=0;
Size:=Length(Value);
for i:=Size downto 1 do
begin
if Copy(Value,i,1)='1' then
Result:=Result+1/(1 shl i);
end;
end;
//十六进制 to 二进制
function HextoBinary(Hex:string):string;
const
BOX: 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(Hex) downto 1 do
Result:=BOX[StrToInt('$'+Hex[i])]+Result;
end;
//十六进制 to 十进制 浮点型
function HextoFloat(s:string):real;
var b,temp:string;
e:integer;
f:real;
begin
s:=StringReplace(s,' ','',[rfReplaceAll]);
b:=HextoBinary(s);
temp := copy(b,2,8);
e:=BintoInt(temp)-127;
temp := copy(b,10,23);
f := 1+floatBintoInt(temp);
if(copy(b,1,1)='0')then
result := power(2,e)*f //StrToFloat( FormatFloat('0.000',power(2,e)*f) )
else
result :=-power(2,e)*f;
end;
//----------------------------------网摘end------------------------------------
//----------------------------------自己的start----------------------------------
//去掉数字字符串前边无效的0
function TFTools.CleanLeftZero(str:string):string;
var
i,j:Integer;
begin
j:=Length(str);
for i:=1 to j do
begin
if str[i]<>'0' then
begin
Result:=Copy(str,i,j-i+1);
Break;
end;
end;
end;
function TFTools.TenToHex(str:string):string; //10-->16
var
i:Integer;
str1:string;
begin
i:=StrToInt(str);
str1:=IntToHex(i,Length(str));
Result:=CleanLeftZero(str1);
end;
function TFTools.HexToTen(str:string):string; //16-->10
begin
Result:=IntToStr(StrToInt('$'+str));
end;
function TFTools.TenToOct(str:string):string; //10-->8
var
i,j,k:Integer;
str1:string;
begin
i:=StrToInt(str);
j:=i div 8;
k:=i mod 8;
while j>0 do
begin
str1:=IntToStr(k)+str1;
k:=j mod 8;
j:=j div 8;
end;
Result:=IntToStr(k)+str1;
end;
function TFTools.OctToTen(str:string):string; //8-->10
var
i,j:Integer;
begin
j:=0;
for i:=1 to Length(str) do
j:=j*8+StrToInt(str[i]);
Result:=IntToStr(j);
end;
function TFTools.TenToBin(str:string):string; //10-->2
var
i,j,k:Integer;
str1:string;
begin
i:=StrToInt(str);
j:=i div 2;
k:=i mod 2;
while j>0 do
begin
str1:=IntToStr(k)+str1;
k:=j mod 2;
j:=j div 2;
end;
Result:=IntToStr(k)+str1;
end;
function TFTools.BinToTen(str:string):string; //2-->10
var
i,j:Integer;
str1:string;
begin
j:=0;
for i:=1 to Length(str) do
j:=j*2+StrToInt(str[i]);
Result:=IntToStr(j);
end;
//----------------------------------自己的end----------------------------------
请发表评论