我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

delphi进制转换-创新互联

----二进制到16进制

成都创新互联专注于企业网络营销推广、网站重做改版、龙门网站定制设计、自适应品牌网站建设、H5页面制作商城开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为龙门等各大城市提供网站开发制作服务。

function BinaryToHex(strBinary: string): string;
var
 vD: Byte;
 i: integer;
 vHextStr: String;
 vP: PChar;
 vLen: integer;
begin
 vLen := length(strBinary);
 if vLen mod 4 > 0 then
 begin
   SetLength(vHextStr, vLen div 4 + 1);
   vLen := vLen div 4 + 1;
 end
 else
 begin
   SetLength(vHextStr, vLen div 4);
   vLen := vLen div 4;
 end;

 // 初始化
 vD := 0;
 vP := PChar(strBinary) + length(strBinary) - 1;
 i := 0; // 开始计数

 while vP^ <> #0 do
 begin
   if vP^ = '1' then
   begin
     case i of
       0:
         vD := vD + 1;
       1:
         vD := vD + 2;
       2:
         vD := vD + 4;
       3:
         vD := vD + 8;
     end;
   end;

   Dec(vP);
   Inc(i);
   if i = 4 then
   begin
     case vD of
       0 .. 9:
         vHextStr[vLen] := Chr(vD + $30);
       10 .. 15:
         vHextStr[vLen] := Chr(vD - 10 + $41);
     end;
     Dec(vLen);
     i := 0;
     vD := 0;
   end;
 end;

 if i > 0 then
 begin
   case vD of
     0 .. 9:
       vHextStr[vLen] := Chr(vD + $30);
     10 .. 15:
       vHextStr[vLen] := Chr(vD + $41);
   end;
 end;

 Result := vHextStr;
end;

---16进制到二进制

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;

-----十六进制到10进制

function HexToInt2(hex: AnsiString): integer;
var
 i: integer;
 function Ncf(num, f: integer): integer;
 var
   i: integer;
 begin
   Result := 1;
   if f = 0 then
     exit;
   for i := 1 to f do
     Result := Result * num;
 end;
 function HexCharToInt(HexToken: Ansichar): integer;
 begin
   if Ord(HexToken) > 97 then
     HexToken := Ansichar(Chr(Ord(HexToken) - 32));
   Result := 0;
   if (Ord(HexToken) > 47) and (Ord(HexToken) < 58) then { chars 0....9 }
     Result := Ord(HexToken) - 48
   else if (Ord(HexToken) > 64) and (Ord(HexToken) < 71) then { chars A....F }
     Result := Ord(HexToken) - 55;
 end;

var
 a16int: integer;
 ancf: integer;
begin
 Result := 0;
 hex := ansiuppercase(trim(hex));
 if hex = '' then
   exit;
 for i := 1 to length(hex) do
 // result := result + HexCharToInt(hex[i]) * ncf(16, length(hex) - i);
 begin
   a16int := HexCharToInt(hex[i]);
   ancf := Ncf(16, length(hex) - i);
   Result := Result + a16int * ancf;
 end;
end;

--------------------------------------------------------------------------------

delphi中有直接把10进制转换成16进制的函数 :

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

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

unit uConversion;

interface

uses
 SysUtils, Math;

type
 TConversion = class
 public
   // 10 进制 to 2,8,16 进制
   function inttoBitStr(intstr: string): string;
   function IntToHexStr(intstr: string): String; // 10 = 2
   function IntToOStr(intstr: string): string;

   // 2进制 to 10,8,16 进制
   function BittoInt(BitStr: String): LongInt; // 2 = 10
   function BitStrToHextStr(const BitStr: String): String; // 2 = 16
   function BitStrToOStr(const BitStr: String): String; // 2 = 8

   // 16 > 10 2  8 进制
   function HextoIntStr(HexStr: String): string;
   function HexToBitStr(HexStr: string): string;
   function HexToOStr(HexStr: string): string;

   // 八进制转换成二进制字符串
   function OtoBitStr(O: string): string;
   function OtoIntStr(O: string): string;
   function OtoHexStr(O: string): string;
 end;

var
 TC: TConversion;

implementation

{ TConversion }
// 2 进制 to 10 进制

function TConversion.BittoInt(BitStr: String): LongInt;
var
 i, Size: Integer;
begin
 Result := 0;
 Size := Length(BitStr);
 for i := Size downto 1 do
 begin
   // 例如 1010
   if Copy(BitStr, i, 1) = '1' then
     Result := Result + (1 shl (Size - i));
 end;
 // 第二种方法
 // 二进制转换为十进制 start
 {
   VAR
   str : String;
   Int : Integer;
   i : integer;

   Str := UpperCase(Edit1.Text);
   Int := 0;
   FOR i := 1 TO Length(str) DO
   Int := Int * 2 + ORD(str[i]) - 48;
   Edit2.Text:=IntToStr(int);
 }
 // 二进制转换为十进制 end;
 // 第三中方法
 {
   function hextoint(s: string): Double;
   begin
   while Length(s) <>0 do
   begin             //2^(长度-1)次方
   if s[1]='1' then Result:=Result+power(2,Length(s)-1);
   s:=Copy(s,2,Length(s));
   end
   end;
 }
end;

function TConversion.BitStrToHextStr(const BitStr: String): String;
var
 vD: Byte;
 i: Integer;
 vHextStr: String;
 vP: PChar;
 vLen: Integer;
begin
 vLen := Length(BitStr);
 if vLen mod 4 > 0 then
 begin
   SetLength(vHextStr, vLen div 4 + 1);
   vLen := vLen div 4 + 1;
 end
 else
 begin
   SetLength(vHextStr, vLen div 4);
   vLen := vLen div 4;
 end;

 // 初始化
 vD := 0;
 vP := PChar(BitStr) + Length(BitStr) - 1;
 i := 0; // 开始计数

 while vP^ <> #0 do
 begin
   if vP^ = '1' then
   begin
     case i of
       0:
         vD := vD + 1;
       1:
         vD := vD + 2;
       2:
         vD := vD + 4;
       3:
         vD := vD + 8;
     end;
   end;

   Dec(vP);
   Inc(i);
   if i = 4 then
   begin
     case vD of
       0 .. 9:
         vHextStr[vLen] := Chr(vD + $30);
       10 .. 15:
         vHextStr[vLen] := Chr(vD - 10 + $41);
     end;
     Dec(vLen);
     i := 0;
     vD := 0;
   end;
 end;

 if i > 0 then
 begin
   case vD of
     0 .. 9:
       vHextStr[vLen] := Chr(vD + $30);
     10 .. 15:
       vHextStr[vLen] := Chr(vD + $41);
   end;
 end;

 Result := vHextStr;
end;

function TConversion.BitStrToOStr(const BitStr: String): String;
var
 vD: Byte;
 i: Integer;
 vHextStr: String;
 vP: PChar;
 vLen: Integer;
begin
 vLen := Length(BitStr);
 if vLen mod 3 > 0 then
 begin
   SetLength(vHextStr, vLen div 3 + 1);
   vLen := vLen div 3 + 1;
 end
 else
 begin
   SetLength(vHextStr, vLen div 3);
   vLen := vLen div 3;
 end;

 // 初始化
 vD := 0;
 vP := PChar(BitStr) + Length(BitStr) - 1;
 i := 0; // 开始计数

 while vP^ <> #0 do
 begin
   if vP^ = '1' then
   begin
     case i of
       0:
         vD := vD + 1;
       1:
         vD := vD + 2;
       2:
         vD := vD + 4;
     end;
   end;

   Dec(vP);
   Inc(i);
   if i = 3 then
   begin
     case vD of
       0 .. 9:
         vHextStr[vLen] := Chr(vD + $30);
     end;
     Dec(vLen);
     i := 0;
     vD := 0;
   end;
 end;

 if i > 0 then
 begin
   case vD of
     0 .. 9:
       vHextStr[vLen] := Chr(vD + $30);
   end;
 end;

 Result := vHextStr;
end;

function TConversion.HexToBitStr(HexStr: string): string;
const
 cBitStrings: 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
 Result := '';
 for i := 1 to Length(HexStr) do
   Result := Result + cBitStrings[StrToIntDef('$' + HexStr[i], 0)];
 while Pos('0', Result) = 1 do
   Delete(Result, 1, 1);
end; { HexToBit }

function TConversion.HextoIntStr(HexStr: String): string;
begin
 Result := IntToStr(StrToInt('$' + (HexStr)));
end;

function TConversion.HexToOStr(HexStr: string): string;
begin
 Result := BitStrToOStr(HexToBitStr(HexStr));
end;

function TConversion.inttoBitStr(intstr: string): string;
var
 i: Integer;
begin
 i := StrToInt(intstr);
 while i <> 0 do
 begin // i mod 2取模,再使用format格式化
   Result := Format('%d' + Result, [i mod 2]);
   i := i div 2
 end
end;
// 10进制装换 2进制 第二种方法
{ function TConversion.IntToBitStr(Value, 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; }

function TConversion.IntToHexStr(intstr: string): String;
begin
 Result := inttoBitStr(intstr);
end;

function TConversion.IntToOStr(intstr: string): string;
begin
 Result := BitStrToHextStr(inttoBitStr(intstr));
end;

function TConversion.OtoBitStr(O: string): string;
const
 cBitStrings: array [0 .. 7] of string = ('000', '001', '010', '011', '100',
   '101', '110', '111');
var
 i, j: Integer;
begin
 Result := '';
 for i := 1 to Length(O) do
 begin
   j := StrToInt(O[i]);
   Result := Result + cBitStrings[j];
 end;
 while Pos('0', Result) = 1 do
   Delete(Result, 1, 1);
end;

function TConversion.OtoHexStr(O: string): string;
begin
 Result := BitStrToHextStr(OtoBitStr(O));
end;

function TConversion.OtoIntStr(O: string): string;
begin
 Result := OtoIntStr(OtoBitStr(O));
end;

end.

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:delphi进制转换-创新互联
文章来源:http://mswzjz.cn/article/cccoos.html

其他资讯