Converting IPv4 to IPv6 and back
March 19, 2009Converting 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 192.168.99.1
- 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 : 192.168.99.1 and convert it to Hex.
Step1 >
Divide the first octet (192) by 16 (since Hex is a Base-16)
IE : 192/16 = 12 times exactly with 0 left over
– 12 in Hex is represented as C
– 0 (zero) in Hex is, you guessed it, 0
Thus 192 in HEX is C0
Step2 >
Repeat step 1 with the second octet (168),
IE : 168/16 = 10 times with 8 left over because 10*6 = 160,
– 10 in HEX is A
– 8 in HEX is 8
Thus 168 in HEX is A8
Step3 >
Repetition rules!!! Third octet (99)
IE : 99/16 = 6 times with 3 left over
– 6 in HEX is 6
– 3 in HEX is 3
Thus 99 in HEX is 63
Step4 >
Last octet
IE : 1/16 = 0 times with 1 left over
– 0 in HEX is, yeah it is 0
– 1 in HEX is 1
Thus 1 in HEX is 01
So the IPv4 address of 192.168.99.1, represented in the IPv6 address portion would be C0A8:6301.
So when using IPv6 6-to-4 Tunnels, on the one endpoint of the tunnel, with the IPv4 address of 192.168.99.1, the complete IPv6 address would be 2002:C0A8:6301::1/64
See, not all that difficult, if you know your 16 multiplication table, you can do this in your head, no problems.
– – – –
.
Converting back from IPv6 to IPv4
Now to convert the same portion of the IPv6address 2002:C0A8:6301::1/64 back to IPv4, the reverse method would apply.
Let me point one more thing about Base-16 out to understand why I’m doing what I am below:
16^{0} = 1
16^{1 }= 16
Taking the portion C0A8:6301, first divide the address into 2 Tuple-groupings (2 Hex Characters) = C0 A8 63 01
Step1 >
Take C0 and multiply the first character ‘C’ by 16 and the second character ‘0’ by 1.
Add the two decimal values together to get the IPv4 decimal value
IE: ((C=12)*16) + (0*1) = 192
Step2 >
Repeat the same process with A8,
IE: ((A=10)*16) + (8*1) = 168
Step3 >
Repeat the same process with 63,
IE: (6*16) + (3*1) = 99
Step4 >
Repeat the same process with 01,
IE: (0*16) + (1*1) = 1
This will give you an IPv4 address of 192.168.99.1
Easy, easy!
very easy method.gud yaar…
by sandip kumar April 18, 2012 at 11:34thanks very insightful
by daniel June 7, 2013 at 19:49However, 192.168.xx.xx is a private address that should not be forwarded on an external IPv6 network…
by Hubert Kirrmann August 11, 2013 at 14:59Thanks a lot
by Napster October 21, 2013 at 04:23Memorize what number stands above 1,2,3,4… F and you will do ipv6 in your head, instead of wasting time
0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256
by asd December 24, 2014 at 00:160 1 2 3 4 5 6 7 8 9 A B C D E F
SO much easier to take each number, write out in binary, split the binary in half, and then add the four bits.
For example, 192 would be 11000000, right? Now break that in half 1100 | 0000 (and forget what the bits represent or what position they’re in 128|64|32|16|8|4|2|1 for a second).
Working with the first four bits (1100). It’s so easy to convert that to 12 (or C) = 8+4+0+0 (Consider those bit now in the 8|4|2|1 place).
Now take the other four bits…0000 = 0. So you quickly have C0 for 192.
In other words… 192
11000000
1100 | 0000 (put a line in the middle)
8 4 2 1 | 8 4 2 1 (new bit placement)
1 1 0 0 | 0 0 0 0 (binary of 192)
12 | 0
C | 0
Again for 168
by Corey King December 9, 2013 at 18:4610101000 (in binary)
1010 | 1000 (put a line in the middle)
8421 | 8421 (new bit placement)
8+2 | 8
10(A) | 8
A8
For me is more simple to work with bits (this method). Less calculation
by MageCliff December 7, 2014 at 08:57I agree. IP4 and IP6 are just human readable representations of the underlying binary address, so working in binary makes perfect sense and is so much easier to visualise.
In effect, the OP’s method is a dec-to-hex converter.
If you’re coding, most (if not all) languages have built in functions for that. For example, in VB,Net:
Dim IP4Parts() = “192.168.99.1”.Split(“.”)
Dim IP6 As String = “”
For p = 0 To 3
IP6 &= Convert.ToInt32(IP4Parts(p)).ToString(“X2”) & IIf(p = 1, “:”, “”)
Next
Console.WriteLine(IP6)
Outputs:
C0A8:6301
And all it does it convert the IP4 dotted decimal in hex and chucks a colon in the right place.
If you’re not coding, just use the Hex function on a calculator (like the one in Windows):
192 = C0
168 = A8
99 = 63
01 = 1
No need for all the maths.
That’s even easier!
by Yowser March 10, 2015 at 10:09good! I am understand on how conversion are made. Thanks!
by Beckham Mwailafu February 9, 2014 at 06:48Reblogged this on staniislaus and commented:
by mrsugbome November 8, 2014 at 17:44smashing!
Thanks you so much!
by asd December 24, 2014 at 00:14thanks guys it helped a lot
by ziggy March 19, 2015 at 02:56But why 2002 is prefixed in front of IPv6 address
by amit October 12, 2015 at 07:51how to convert IPv6 to decimal
by Ritesh Raghuvanshi October 13, 2015 at 02:36Special Thanks
by Moe Moe March 16, 2017 at 00:37