Type TIPFormat = (ifV4, ifV6); TIPV4 = array[0..3] of byte; TIPV6 = array[0..15] of byte; // accept 0.0.0.0 to 255.255.255.255 function StrToIPV4(const S: string): TIPV4; var L, N, I, U : integer; const CTB : array['0'..'9'] of byte = (0,1,2,3,4,5,6,7,8,9); begin L := Length(S); Assert((L > 6) and (L < 16), 'Error : string is not IPV4.'); I := 0; U := 0; for N := 1 to Length(S) do begin case S[N] of '0'..'9': begin case U of 0 : result[I] := CTB[S[N]]; 1..2 : result[I] := CTB[S[N]] + (result[I]*10); end; inc(U); assert(U < 4, 'Error : string is not IPV4.'); end; '.' : begin inc(I); U := 0; end; end; end; end; // return 0.0.0.0 to 255.255.255.255 function IPV4ToStr(const IP: TIPV4): string; begin result := format( '%d.%d.%d.%d', [ IP[0],IP[1],IP[2],IP[3] ] ); end; // accept 0000:0000:0000:0000:0000:0000:0000:0000 to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff function StrToIPV6(const S: string): TIPV6; var N,I,H,L : integer; const CNTB : array['0'..'9'] of byte = (0,1,2,3,4,5,6,7,8,9); CHTB : array['a'..'f'] of byte = ($a,$b,$c,$d,$e,$f); begin L := Length(S); Assert(L = 39, 'Error: string is not IPV6'); I := 0; H := 0; for N := 1 to L do begin case S[N] of '0'..'9' : begin case H of 0 : result[I] := CNTB[S[N]]; 1 : result[I] := (result[I] shl 4) or CNTB[S[N]]; end; inc(H); end; 'a'..'f' : begin case H of 0 : result[I] := CHTB[S[N]]; 1 : result[I] := (result[I] shl 4) or CHTB[S[N]]; end; inc(H); end; end; if H > 1 then begin inc(I); H := 0; end; end; end; // return 0000:0000:0000:0000:0000:0000:0000:0000 to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff function IPV6ToStr(const IP: TIPV6): string; const HXTSTR : array[0..$F] of char = '0123456789abcdef'; begin SetLength(result, 39); //loop unfolded result[1] := HXTSTR[IP[0] shr 4]; result[2] := HXTSTR[IP[0] and $F]; result[3] := HXTSTR[IP[1] shr 4]; result[4] := HXTSTR[IP[1] and $F]; result[5] := ':'; result[6] := HXTSTR[IP[2] shr 4]; result[7] := HXTSTR[IP[2] and $F]; result[8] := HXTSTR[IP[3] shr 4]; result[9] := HXTSTR[IP[3] and $F]; result[10] := ':'; result[11] := HXTSTR[IP[4] shr 4]; result[12] := HXTSTR[IP[4] and $F]; result[13] := HXTSTR[IP[5] shr 4]; result[14] := HXTSTR[IP[5] and $F]; result[15] := ':'; result[16] := HXTSTR[IP[6] shr 4]; result[17] := HXTSTR[IP[6] and $F]; result[18] := HXTSTR[IP[7] shr 4]; result[19] := HXTSTR[IP[7] and $F]; result[20] := ':'; result[21] := HXTSTR[IP[8] shr 4]; result[22] := HXTSTR[IP[8] and $F]; result[23] := HXTSTR[IP[9] shr 4]; result[24] := HXTSTR[IP[9] and $F]; result[25] := ':'; result[26] := HXTSTR[IP[10] shr 4]; result[27] := HXTSTR[IP[10] and $F]; result[28] := HXTSTR[IP[11] shr 4]; result[29] := HXTSTR[IP[11] and $F]; result[30] := ':'; result[31] := HXTSTR[IP[12] shr 4]; result[32] := HXTSTR[IP[12] and $F]; result[33] := HXTSTR[IP[13] shr 4]; result[34] := HXTSTR[IP[13] and $F]; result[35] := ':'; result[36] := HXTSTR[IP[14] shr 4]; result[37] := HXTSTR[IP[14] and $F]; result[38] := HXTSTR[IP[15] shr 4]; result[39] := HXTSTR[IP[15] and $F]; end;
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.