Take the following IPv4 address: 128.32.10.1
This address has 4 octets where each octet is a single byte (or 8 bits).
128
has the binary representation: 10000000
32
has the binary representation: 00100000
10
has the binary representation: 00001010
1
has the binary representation: 00000001
So 128.32.10.1
== 10000000.00100000.00001010.00000001
Because the above IP address has 32 bits, we can represent it as the unsigned 32 bit number: 2149583361
Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address.
Examples
2149583361 ==> "128.32.10.1"
32 ==> "0.0.0.32"
0 ==> "0.0.0.0"
(요약) 주어진 숫자를 IPv4로 변환하라.
function int32ToIp(int32){ const ipv4 = []; while(ipv4.length < 4) { ipv4.unshift(int32 % 256); int32 = (int32 / 256)|0; } return ipv4.join('.'); }
ip주소는
0 ~ 255
사이의 숫자 4개가.
을 사이에 두고 표현된다.
예를 들면0.0.0.0
,192.168.0.1
같은 형식이다.
그래서 결국256
진법을 4자리로 표현하는 방법으로 구하면 된다.