本文整理自网络,侵删。
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 xe5 android 调用 google zxing
Delphi string.split 按照任意字符串分割语句
Delphi xe 新功能试用:多种皮肤样式静、动态设置方法
Delphi 关于 class helper for ... 语法
关于Delphi xe5 firemonkey 手机屏幕自适应程序问题
更多相关阅读请进入《Delphi》频道 >>