Lesson 2 of 15

Binary & Hexadecimal

Binary & Hexadecimal in Networking

Network addresses and data are fundamentally binary. Understanding binary and hexadecimal notation is essential for working with IP addresses, subnet masks, and MAC addresses.

Binary Representation

Each IPv4 octet is an 8-bit binary number:

192 = 11000000
168 = 10101000
  1 = 00000001

Hexadecimal

Hexadecimal (base 16) uses digits 0-9 and letters A-F. Each hex digit represents 4 bits:

0x0 = 0000    0x8 = 1000
0x1 = 0001    0x9 = 1001
0x2 = 0010    0xA = 1010
0x3 = 0011    0xB = 1011
0x4 = 0100    0xC = 1100
0x5 = 0101    0xD = 1101
0x6 = 0110    0xE = 1110
0x7 = 0111    0xF = 1111

MAC Addresses

MAC addresses are 48-bit hardware addresses written in hexadecimal: AA:BB:CC:DD:EE:FF

Your Task

Implement three functions:

  • ipToBinary(ip) — converts an IPv4 string to its 32-bit binary representation (dotted, 8 bits per octet)
  • ipToHex(ip) — converts an IPv4 string to hexadecimal (e.g., "C0A80101")
  • binaryToIp(bin) — converts a dotted binary string back to an IPv4 address
Node.js loading...
Loading...
Click "Run" to execute your code.