Converting IPv4 to IPv6 and back

Converting from IPv4 to IPv6

is so easy, yet everyone seem to convert a IPv4 address to binary, then to IPv6. Why? Why waste time and do things the long way? Not cool.

When would you need to do this? One specific use is IPv6 6-to-4 tunnels, which always concatenates 2002::/16 with the IPv4 address embedded.
With Automatic 6-to-4-tunnels, your address format is as follow:
2002:<32 bit IPv4 site address in Hex>:<16 bit network number in Hex>::/64

The question is how to do the conversion.

Firstly before starting I will assume everyone knows the following:

  • Binary is a Base-2 numbering system, as it has only 0,1
  • Decimal is a Base-10 numbering system, as it has 0,1,2,3,4,5,6,7,8,9
  • Hexadecimal is a Base-16 numbering system, as it has 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

I also assume you know the hex values in decimal:

A = 10
B = 11
C = 12
D = 13
E = 14
F = 15

Two more things I would like to mention before explaining the conversion.
An IPv4 address : example

  • Each Octet (8 bits) “between the dot-thingys” denote 1 byte

An IPv6 address : example 2001:0db8:85a3:0000:0000:8a2e:0370:7334

  • Two Tuples (1 Tuple = 4 bits = 1 Hex character) denotes 1 byte

Then converting is easy. Lets take the following IPv4 address : and convert it to Hex.

Continue reading “Converting IPv4 to IPv6 and back”