Delphi中的进制转换
1.HexToBin() 十六進制轉(zhuǎn)換二進制
所在單元:Classes
Delphi語法:
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer
描述:
調(diào)用HexToBin函數(shù)轉(zhuǎn)換十六進制字符串到相應(yīng)的二進制值。
Text是一個表示十六進制值的字符串。
Buffer返回轉(zhuǎn)換后的二進制結(jié)果值。
BufferSize表示Buffer的大小。Text需要指向至少2*BufSize的十六進制字符,因為每兩個十六進制字符表現(xiàn)為一個字節(jié)。
HexToBin返回在Buffer中因為Text沒有包含有效的十六進制字符('0'..'f')而還沒有被用的字符數(shù)量.
注意:十六進制數(shù)必須使用小寫字符;HexToBind不能識別大寫字符。
?
2.BinToHex() 二進制轉(zhuǎn)換十六進制
所在單元:Classes
Delphi語法:
procedure BinToHex(Buffer, Text: PChar; BufSize: Integer);
描述:
調(diào)用BinToHex轉(zhuǎn)換buffer中的二進制值為它所表示的十六進制字符串
Buffer是一個字節(jié)的緩沖區(qū),其中包含二進制值
Text返回一個以null為結(jié)束字符的字符串,表示Buffer作為十六進制數(shù)的值
BufSize表示Buffer的大小。Text需要指向一系列字符,這些字符至少有2*BufSize大小字節(jié)。
?
3.IntToHex()將整型數(shù)轉(zhuǎn)換為十六進制數(shù)
所在單元:SysUtils
Delphi語法:
function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;
描述:
IntToHex轉(zhuǎn)換一個數(shù)字為這個數(shù)字十六進制表示的字符串。Value是要轉(zhuǎn)換的數(shù)字。參數(shù)Digits指定字符最小寬度,最小寬度不足時將用0填充。
?
4.StrToInt()字符串轉(zhuǎn)換成整型數(shù)
所在單元:SysUtils
Delphi語法:
function StrToInt(const S: string): Integer;
描述:
返回字符串S轉(zhuǎn)換成整數(shù),字符串非整數(shù)表達時將引起異常,十六進制字符串轉(zhuǎn)換為整型數(shù)要求在字符串前面添加$即可。
?
5.把一個整數(shù)變成二進制字符串
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.十六進制轉(zhuǎn)換二進制
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.八進制和十進制的轉(zhuǎn)換
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;?
//----------------------------------網(wǎng)摘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;
//----------------------------------網(wǎng)摘end------------------------------------
//----------------------------------自己的start----------------------------------
//去掉數(shù)字字符串前邊無效的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----------------------------------
轉(zhuǎn)載于:https://www.cnblogs.com/760044827qq/p/3806439.html
超強干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的Delphi中的进制转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Yii 之分页 + bootstrap
- 下一篇: jQuery学习之:Validation