|
//********************************************************* //16转10,否则输出-1 //********************************************************* function Hex(c: char): Integer; var x: Integer; begin if ( Ord(c)>= Ord('0')) and (Ord(c) <= Ord('9')) then x:= Ord(c) - Ord('0') else if (Ord(c) >= Ord('a')) and (Ord(c) <= Ord('f')) then x:= Ord(c) - Ord('a') + 10 else if (Ord(c) >= Ord('A')) and (Ord(c) <= Ord('F')) then x:= Ord(c) - Ord('A') + 10 else x:= -1; //输入错误 Result:= x; end;
//******************************************************************* //该函数接收1~2个,字符转换成功后输出对应10进制数的值,否则输出-1 //******************************************************************* function HexToInt(Str: string): Integer; var tmpInt1, tmpInt2: Integer; begin if Length(Str) = 1 then begin Result:= Hex(Str[1]); end else if Length(Str) = 2 then begin tmpInt1:= Hex(Str[1]); tmpInt2:= Hex(Str[2]); if (tmpInt1 = -1) or (tmpInt2 = -1) then Result:= -1 else Result:= tmpInt1 * 16 + tmpInt2; end else Result:= -1; //输入错误,转换失败 end;
//**************************** //字符串转换成ASCII码字符串 //**************************** function StrToHexStr(const S: string): string; var i: Integer; begin for i:= 1 to Length(S) do begin if i = 1 then Result:= IntToHex(Ord(S[1]), 2) else Result:= Result + ' ' + IntToHex(Ord(S[i]), 2); end; end;
//*************************** //该函数去掉字符串中所有空格 //*************************** function TrimAll(Str: string): string; var mLen, i: Integer; begin mLen:= Length(Str); //初始化返回值 Result:= ''; for i:= 0 to mLen do begin //是空格就去掉 if Str[i] = '' then Continue; Result:= Result + Str[i]; end; end;
//*************************** //十进制转换成二进制字符串 //*************************** function DTob(decimal:longint):string; const symbols:string[16]='01'; var scratch:string; remainder:byte; begin repeat remainder:=decimal mod 2; scratch:=symbols[remainder+1]+scratch; decimal:=decimal div 2; until(decimal=0); result:=scratch; end;
//*************************** //十进制转换成十六进制字符串 //***************************
function DToHex(decimal:longint):string; const symbols:string[16]='0123456789abcdef'; var scratch:string; remainder:byte; begin repeat remainder:=decimal mod 16; scratch:=symbols[remainder+1]+scratch; decimal:=decimal div 16; until(decimal=0); result:=scratch; end;
(阅读次数:
)
|