125 lines
2.5 KiB
ObjectPascal
125 lines
2.5 KiB
ObjectPascal
![]() |
unit U_HexFunction;
|
|||
|
|
|||
|
interface
|
|||
|
uses
|
|||
|
SysUtils,Windows,forms,Spcomm, shlobj,ExtCtrls;
|
|||
|
function HexStrToStr(const S:string):string; // ʮ<><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
function StrToHexStr(const S:string):string;
|
|||
|
function bintoint01(s: string): Double; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD>
|
|||
|
function hextoint01(s: string): Integer; //ʮ<><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD>
|
|||
|
Function intTobin01(Value :Integer): string; //ʮ<><CAAE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Function intTohex01(value :integer): string; //ʮ<><CAAE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
implementation
|
|||
|
uses Math;
|
|||
|
|
|||
|
Function intTohex01(value :integer) : string;
|
|||
|
begin
|
|||
|
result:=uppercase(format('%x',[value]));
|
|||
|
end;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>תʮ<D7AA><CAAE><EFBFBD><EFBFBD>
|
|||
|
Function binToDec(Value :string) : string;//integer;
|
|||
|
VAR
|
|||
|
str : String;
|
|||
|
Int : Integer;
|
|||
|
i : integer;
|
|||
|
BEGIN
|
|||
|
Str := UpperCase(Value);
|
|||
|
Int := 0;
|
|||
|
FOR i := 1 TO Length(str) DO
|
|||
|
Int := Int * 2+ ORD(str[i]) - 48;
|
|||
|
Result := IntToStr(Int);
|
|||
|
end;
|
|||
|
|
|||
|
//ʮ<><CAAE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>һ
|
|||
|
function mod_num(n1,n2:integer):integer;//ȡ<><C8A1><EFBFBD><EFBFBD>
|
|||
|
begin
|
|||
|
result:=n1-n1 div n2*n2
|
|||
|
end;
|
|||
|
//ʮ<><CAAE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
function reverse(s:String):String;//ȡ<><C8A1><EFBFBD><EFBFBD>
|
|||
|
Var
|
|||
|
i,num:Integer;
|
|||
|
st:String;
|
|||
|
begin
|
|||
|
num:=Length(s);
|
|||
|
st:='';
|
|||
|
For i:=num DownTo 1 do
|
|||
|
Begin
|
|||
|
st:=st+s[i];
|
|||
|
End;
|
|||
|
Result:=st;
|
|||
|
end;
|
|||
|
|
|||
|
//ʮ<><CAAE><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
Function intTobin01(Value:Integer) : string;
|
|||
|
Var
|
|||
|
ST:String;
|
|||
|
N:Integer;
|
|||
|
Begin
|
|||
|
ST:='';
|
|||
|
n:=value;
|
|||
|
While n>=2 Do
|
|||
|
Begin
|
|||
|
st:=st+IntToStr(mod_num(n,2));
|
|||
|
n:=n div 2;
|
|||
|
End;
|
|||
|
st:=st+IntToStr(n);
|
|||
|
Result:=reverse(st);
|
|||
|
End;
|
|||
|
function HexStrToStr(const S:string):string;
|
|||
|
//16<31><36><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
var
|
|||
|
t:Integer;
|
|||
|
ts:string;
|
|||
|
M,Code:Integer;
|
|||
|
begin
|
|||
|
t:=1;
|
|||
|
Result:='';
|
|||
|
while t<=Length(S) do
|
|||
|
begin //xlh 2006.10.21
|
|||
|
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
|
|||
|
inc(t);
|
|||
|
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
|
|||
|
ts:='$'+S[t]
|
|||
|
else
|
|||
|
ts:='$'+S[t]+S[t+1];
|
|||
|
Val(ts,M,Code);
|
|||
|
if Code=0 then
|
|||
|
Result:=Result+Chr(M);
|
|||
|
inc(t,2);
|
|||
|
end;
|
|||
|
end;
|
|||
|
|
|||
|
function StrToHexStr(const S:string):string;
|
|||
|
//<2F>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>16<31><36><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
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;
|
|||
|
|
|||
|
|
|||
|
//ʮ<><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(S)-->>ʮ<><CAAE><EFBFBD><EFBFBD>(I)
|
|||
|
function hextoint01(s: string): Integer;
|
|||
|
begin //$<24><><EFBFBD><EFBFBD>16<31><36><EFBFBD><EFBFBD>
|
|||
|
Result:=StrToInt('$'+s);
|
|||
|
end;
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(S)-->>ʮ<><CAAE><EFBFBD><EFBFBD>(D) [<5B><>д:Jey]
|
|||
|
function bintoint01(s: string): Double;
|
|||
|
begin
|
|||
|
while Length(s) <>0 do
|
|||
|
begin //2^(<28><><EFBFBD><EFBFBD>-1)<29>η<EFBFBD>
|
|||
|
if s[1]='1' then Result:=Result+power(2,Length(s)-1);
|
|||
|
s:=Copy(s,2,Length(s));
|
|||
|
end
|
|||
|
end;
|
|||
|
|
|||
|
end.
|