Browse Source

IP address encode/decode test.

pull/50/head
Rafal Kupiec 2 years ago
parent
commit
46250f0cd8
Signed by: belliash GPG Key ID: 4E829243E0CFE6B4
2 changed files with 26 additions and 0 deletions
  1. +22
    -0
      tests/ip_addr_enc_dec.aer
  2. +4
    -0
      tests/ip_addr_enc_dec.exp

+ 22
- 0
tests/ip_addr_enc_dec.aer View File

@ -0,0 +1,22 @@
class Program {
string encode_ip(string $dotquad_ip) {
string[] $ip_sep = explode('.', $dotquad_ip);
return sprintf('%02x%02x%02x%02x', $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]);
}
string decode_ip(string $int_ip) {
string[] $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
return hexdec($hexipbang[0]) + '.' + hexdec($hexipbang[1]) + '.' + hexdec($hexipbang[2]) + '.' + hexdec($hexipbang[3]);
}
void main() {
string $localhost = $this->encode_ip('127.0.0.1');
print("127.0.0.1 ==> $localhost\n");
string $router = $this->encode_ip('192.168.2.1');
print("192.168.2.1 ==> $router\n");
print("$localhost ==> ", $this->decode_ip($localhost) + PHP_EOL);
print("$router ==> ", $this->decode_ip($router) + PHP_EOL);
}
}

+ 4
- 0
tests/ip_addr_enc_dec.exp View File

@ -0,0 +1,4 @@
127.0.0.1 ==> 7f000001
192.168.2.1 ==> c0a80201
7f000001 ==> 127.0.0.1
c0a80201 ==> 192.168.2.1

Loading…
Cancel
Save