_uc_ip_codecs.pas

unit _uc_ip_codecs;

interface

  uses classes,SysUtils;

Type

    TIpCodecs = class
    public
    class function ip_to_int(ip:string):string;
    class function int_to_ip(ip:string):string;
    class function Encry(const S: String): String;
    class function Decry(const S: String): String;
    class function HTTPEncode(const AStr: string): string;
    class function HTTPDecode(const AStr: string): string;
    class function _EnCodeTheHEXStringV2(s:string):String;
    class function _DeCodeTheHEXStringV2(s:string):String;
    end;

implementation

    uses _uc_refs_consts;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
class function TIpCodecs.ip_to_int(ip:string):string;         // 255.255.255.255 to >>  2154578
var
ipxx:TStringList;
begin

ipxx:=TStringList.Create;
try
  //(aaa*16777216)+(bbb*65536)+(ccc*256)+ddd
  ipxx.Text:=StringReplace(ip,'.',ret,[rfReplaceAll]);
  if ipxx.Count<>4 then exit;
  //direct integer
  result:=inttostr( (strtoint64(ipxx[0])*16777216) + (strtoint64(ipxx[1])*65536) + (strtoint64(ipxx[2])*256) + strtoint(ipxx[3]));

finally ipxx.Free; end;

end;


//------------------------------------------------------------------------------
class function TIpCodecs.int_to_ip(ip:string):string;         // 2154578 to >> 255.255.255.255
var
i:integer;
e:int64;
pp: array[0..3] of byte;
begin

e:=StrToInt64Def(ip,0);

try
 for i:=0 to 3 do
 begin
 pp[i] :=  e shr (24 - (i*8));
 e:=e- (pp[i] shl (24 - (i*8)));
 end;
result:=format('%d.%d.%d.%d',[pp[0],pp[1],pp[2],pp[3]]);
except result:='0.0.0.0'; end;
end;

//------------------------------------------------------------------------------
class function TIpCodecs.Encry(const S: String): String;
var
i: Integer;
begin
//hexA        ='ABCDEF';
//hexB        ='456789';
Result := S;
  for i := 1 to 6 do
  begin
  Result:=StringReplace(result,hexB[i],hexA[i],[rfReplaceAll]);
  end;

end;

//------------------------------------------------------------------------------
class function TIpCodecs.Decry(const S: String): String;
var
I: Integer;
begin

Result := S;
  for I := 1 to 6 do
  begin
  Result:=StringReplace(result,hexA[i],hexB[i],[rfReplaceAll]);
  end;

end;


//------------------------------------------------------------------------------
class function TIpCodecs.HTTPEncode(const AStr: String): String;
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-'];
var
Sp, Rp: PChar;
begin

  SetLength(Result, Length(AStr) * 3);
  Sp := PChar(AStr);
  Rp := PChar(Result);

  while Sp^ <> #0 do
  begin
    if Sp^ in NoConversion then Rp^ := Sp^
    else if Sp^ = ' ' then Rp^ := '+'
                       else begin
                            FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
                            Inc(Rp,2);
                            end;
  Inc(Rp);
  Inc(Sp);
  end;

  SetLength(Result, Rp - PChar(Result));

end;
//------------------------------------------------------------------------------
class function TIpCodecs.HTTPDecode(const AStr: string): string;
var
a, b, c: PChar;
begin

SetLength(Result, Length(AStr));
a := PChar(AStr);
b := PChar(Result);

  while (a^ <> #0) do
  begin
        case a^ of
        '+': b^ := ' ';
             '%': begin
                  inc(a);
                  if (a^ = '%') then
                  b^ := '%' else begin
                                 c := a;
                                 Inc(a);
                                 b^ := Chr(StrToInt(Format('$%s%s',[c^, a^])));
                                 end;
                  end;
        else
        b^ := a^;
        end;
        Inc(b);
        Inc(a);
  end;
SetLength(Result, b - PChar(Result));

end;




//------------------------------------------------------------------------------
class function TIpCodecs._EnCodeTheHEXStringV2(s:string):String;
var
i:integer;
begin

  for i:=1 to length(s) do
  begin
  Result:=Result+IntToHex(Ord(s[i])-4,2);   // :) -4
  end;

end;

//------------------------------------------------------------------------------
class function TIpCodecs._DeCodeTheHEXStringV2(s:string):String;
var
i,L:integer;
e:string[2];
begin

L:=Length(s);
if Odd(L) then begin Result:='ErrorInput!'; exit; end;



       for i:=1 to L do
       if Odd(i) then
       begin
       e:=s[i]+s[i+1];
       Result:=Result+Chr(StrToIntDef('$'+e,0)+4);
       end;


end;


end.






../delphi71code/exosee-code-source/uc_ip_codecs-pas..