Delphi 收集了比较全的字符串进制转换


本文整理自网络,侵删。

 
function RightStr
    (Const Str: String; Size: Word): String;
begin
  if Size > Length(Str) then Size := Length(Str) ;
  RightStr := Copy(Str, Length(Str)-Size+1, Size)
end;
function MidStr
    (Const Str: String; From, Size: Word): String;
begin
  MidStr := Copy(Str, From, Size)
end;
function LeftStr
    (Const Str: String; Size: Word): String;
begin
  LeftStr := Copy(Str, 1, Size)
end;



//--------------------此函数可把汉字转化为16进制字符串输出--------------------------------------
function ChineseToHex(Chinese:String):String;
begin
  result:=InttoHex(ord(Chinese[1]),2)+InttoHex(ord(Chinese[2]),2);
end;
//--------------------此函数可把4个16进制字符串转化成一个汉字输出--------------------------------------
function HextoChinese(HexStr:String):String;
var
  hi,lo:integer;
begin
  hi:=strtoint('$'+leftstr(HexStr,2));
  lo:=strtoint('$'+rightstr(HexStr,2));
  result:=widestring(char(hi)+char(lo));
end;
//--------------------此函数可把汉字或字符转化成16进制字符串输出--------------------------------------
function ChartoHex(CharStr:String):String;
begin
  if ord(CharStr[1])<128 then
    result:=inttohex(strtoint(inttostr(ord(CharStr[1]))),2)
  else
    result:=InttoHex(ord(CharStr[1]),2)+InttoHex(ord(CharStr[2]),2);
end;



//10进制转16进制
//参数说明:
//Int:被转换的整型值
Function IntToHex(Int: Int64;Size: Integer=0): String;
begin
  result:=System.SysUtils.IntToHex(int,0);
  if Size>0 then
    while length(Result)<Size do
      Result:='0'+Result;
end;

//将一个十六进制字符串转换成十进制数值
Function HexToInt(Hex: string): Int64;
Begin
  Result:=strtoint64('$'+Hex);
End;

//将一个十六进制字符串转换成二进制数值
Function HexToBin(Hex: string): string;
var
  icnt:integer;
  sRes:string;
  sHexOne:string;
begin
  sRes:='';
  for icnt:=length(Hex) downto 1 do
  begin
    sHexOne:=copy(Hex,icnt,1);
    if sHexOne ='0' then sRes:='0000'+sRes
    else if sHexOne ='1' then sRes:='0001'+sRes
    else if sHexOne ='2' then sRes:='0010'+sRes
    else if sHexOne ='3' then sRes:='0011'+sRes
    else if sHexOne ='4' then sRes:='0100'+sRes
    else if sHexOne ='5' then sRes:='0101'+sRes
    else if sHexOne ='6' then sRes:='0110'+sRes
    else if sHexOne ='7' then sRes:='0111'+sRes
    else if sHexOne ='8' then sRes:='1000'+sRes
    else if sHexOne ='9' then sRes:='1001'+sRes
    else if sHexOne ='A' then sRes:='1010'+sRes
    else if sHexOne ='B' then sRes:='1011'+sRes
    else if sHexOne ='C' then sRes:='1100'+sRes
    else if sHexOne ='D' then sRes:='1101'+sRes
    else if sHexOne ='E' then sRes:='1110'+sRes
    else if sHexOne ='F' then sRes:='1111'+sRes;
  end;
  while copy(sRes,1,1)='0' do
    sRes:=copy(sRes,2,length(sRes));

  if sRes='' then
    sRes:='0';

  Result:=sRes;
End;

//二进制转十六进制
Function BinToHex(Bin:string):string;
var
  iLen:integer;
  sBinAll:string;
  sBinOne:string;
  sRes:string;
begin
  if 4-(length(Bin) Mod 4)>0 then
    sBinAll:=copy('0000',1,4-(length(Bin) Mod 4))+Bin
  else
    sBinAll:=Bin;

  iLen:=trunc(length(sBinAll)/4);

  sRes:='';
  while iLen>0 do
  begin
    sBinOne:=copy(sBinAll,length(sBinAll)-3,4);
    if sBinOne ='0000' then sRes:='0'+sRes
    else if sBinOne ='0001' then sRes:='1'+sRes
    else if sBinOne ='0010' then sRes:='2'+sRes
    else if sBinOne ='0011' then sRes:='3'+sRes
    else if sBinOne ='0100' then sRes:='4'+sRes
    else if sBinOne ='0101' then sRes:='5'+sRes
    else if sBinOne ='0110' then sRes:='6'+sRes
    else if sBinOne ='0111' then sRes:='7'+sRes
    else if sBinOne ='1000' then sRes:='8'+sRes
    else if sBinOne ='1001' then sRes:='9'+sRes
    else if sBinOne ='1010' then sRes:='A'+sRes
    else if sBinOne ='1011' then sRes:='B'+sRes
    else if sBinOne ='1100' then sRes:='C'+sRes
    else if sBinOne ='1101' then sRes:='D'+sRes
    else if sBinOne ='1110' then sRes:='E'+sRes
    else if sBinOne ='1111' then sRes:='F'+sRes;
    sBinAll:=copy(sBinAll,1,length(sBinAll)-4);
    iLen:=length(sBinAll);
  end;
  if sRes='' then
    sRes:='0';

  Result:=sRes;
end;

//将一个二进制字符串转换成十进制数值
Function BinToInt(Bin: string): Int64;
Begin
  Result:=HexToInt(BinToHex(Bin));
End;


//将一个十进制整型转换成二进制值
//参数说明:
//Int:被转换的整型值
//Size:转换后的宽度:4位 8位 或更大 设为0则由系统自动判断
Function IntToBin(Int: Int64;Size: Integer=0): String;
Begin
  Result:=HexToBin(System.SysUtils.IntToHex(int,0));
  if Size>0 then
    while length(Result)<Size do
      Result:='0'+Result;
End;

相关阅读 >>

Delphi xe10 文件目录/路径操作 (andorid、ios、windows)

Delphi 如何在twebbrowsers之间传递数据

Delphi 判断系统服务是否运行

Delphi中利用钩子实现qq聊天窗口的修改

Delphi 调用cmd获取java 版本号

indy断点续传

Delphi 中的字符串

Delphi xe5开发的android手机截屏功能

Delphi xe android/ios 实现图片放大缩小的两种方法

Delphi 中英文字符怎么按固定长度截断

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...