You are on page 1of 4

Converting

to Base 256 from Decimal

To Convert a Decimal Number to a Base-256 dotted-decimal

Step
1 Evaluatethenumbertobeconverted:
Ifthedecimalnumberislessthan256,thenusethatdecimalnumberforthe4thoctet,and
padtherestwithzerossothattheresultisindotteddecimalform,withfouroctets.
Ifthenumberis>=256,thencontinuetostep2.
2 Dividethedecimalnumberby256
3 Multiplythedecimalportionoftheresultfromstep2by256
4 Subtracttheresultofstep3fromtheoriginaldecimalnumbertoyieldtheBase256octet
5 Examinethedecimalportionoftheresultfromstep2
a. Ifthatdecimalportionofthenumberislessthan256,thenusethatnumberforthenext
octet.
Usezeroesforanyremainingoctets,sothatthenumberisindotteddecimalform,with4
octets.
b. Ifthatnumberislargerthan,orequalto256,thencontinuefromstep2,usingthatnumber.

Example 1
FindthelastaddressinanIPv4subnetwithanetworkaddressof16.0.0.0/17,with32,768addressesper
subnet.

First,convertthenumberofaddresses1toBase256(dotteddecimal).32,7681=32,767
Oncetheconversioniscomplete,addthatBase256dotteddecimalvaluetothe1staddresstodeterminethe
lastaddressinthesubnet.

Firstaddressinsubnet0(the1stsubnet):
16 . 0 . 0 . 0
Numberofaddresses(32,768persubnet)1:
0 . 0 . 127 . 255
Lastaddressinsubnet0:
16 . 0 . 127 . 255

Convert32,767toanIPv4address.
1. Is32,767largerthan256?Yes.Thenproceedtostep2.
2. 32,767/256=127.996
3. 127x256=32,512
4. 32,76732,512=255(thisisthe4thoctetofthedotteddecimal)

The3rdOctet
1. Since127issmallerthan256youredonedividingandthevalueforthe3rdoctetis127

Usingzeroesforanyremainingoctetsyieldsadotteddecimalvalueof:0.0.127.255
The255isfromstep3,andthe127iswhatwasleftover.

2009Prof.Chin,ConsciousVibes.com
Lastsaved:3/6/201612:33PM
Page1of4

Example 2
Convert2,215,708,686toaBase256dotteddecimalIPv4address.

The 4th Octet is:


1. 2,215,708,686/256 =8,655,112.055
2. 8,655,112x256
=2,215,708,672
3. 2,215,708,6862,215,708,672=14(So,14isthe4thoctet)

The 3rd Octet is:


1. 8,655,112/256
=33,809.031
2. 33,809x256
=8,655,104
3. 8,655,1128,655,104=8 (So,the3rdoctetis8)

The 2nd Octet is:


1. 33,809/256 =132.066
2. 132x256
=33,792
3. 33,80933,792=17 (So,the2ndoctetis17)

The 1st Octet is:


1. 132isleftover.Since132istoosmalltodivideby256,132isthe1stoctet

Theresultis:2,215,708,686=132.17.8.14

Check
132x2563= 2,214,592,512
17x2562=
1,114,112
8x2561=
2,048
0
14x256 =
14

2,215,708,686

Example 3
Convert1,024toBase256

The 4th Octet is:


1. 1,024/256 =4
2. 4x256
=1,024
3. 1,0241,024=0(So,0isthe4thoctet)

The 3rd Octet is:

Sincewereleftwith4,and4islessthan256,weuse4forthe3rdoctet

Theresult:1,024decimalisequalto0.0.4.0inBase256

2009Prof.Chin,ConsciousVibes.com

Lastsaved:3/6/201612:33PM

Page2of4

To Convert an IPv4 address to Decimal

2563
16,777,216
w

2562
65,536
x

2561
256
y

2560
1
z

AnIPv4addressisa32bitnumber.Itisgenerallywritteninthe"dottedquad"notation:w.x.y.z.Toconvertan
IPaddresstobase10,calculatew*16,777,216+x*65,536+y*256+z.
http://www.everything2.com/e2node/Decimal%2520IP%2520address

Example 1
InPHP,tryip2long().

HOWit'sdone.
http://www.cre8asiteforums.com/forums/index.php?showtopic=47466
256^0=1
256^1=256
256^2=65,536
256^3=16,777,216

So,194.247.44.146is...

146*256^0=146
44*256^1=11,264
247*256^2=16,187,392
194*256^3=3,254,779,904

Addthoseup,and...

146+11,264+16,187,392+3,254,779,904=3270978706

Howdoyougobackwardson3270978706toget194.247.44.146?
Beginbydividingby256^3andcountthenumberoftimesyoucandothis.Taketheremainderanddothe
samewith256^2,256^1and256^0.

EG:3270978706/256^3=194remainder:16198802.

Example 2
http://www.everything2.com/e2node/Decimal%2520IP%2520address
2009Prof.Chin,ConsciousVibes.com

Lastsaved:3/6/201612:33PM

Page3of4


InUNIX,thesearehandledwithinet_ntoa()andinet_aton().

ToconvertdecimalIPstonormaldottedquadform(forexample,tospoilspammers'joy),usethisPerlhack:

perlMSockete"printinet_ntoa(pack('N','354267876296'));"

Replacenumberaccordingly.

Reference
DecodedecimalIPaddressto
dottedquad
ConvertandlocateIPaddresses
IPv4AddressTutorial:Converting
OctetstoBinaryandDecimal
HowtoConvertanIPAddressto10
DigitsDecimalNumber
DecimalIPAddress
URLDiscombobulator
Searchfor:convertdecimaltoip

http://toastedspam.com/decodeip
http://kloth.net/services/iplocate.php
http://countryipblocks.net/binary.php
http://ezinearticles.com/?HowtoConvertanIPAddressto10
DigitsDecimalNumber&id=1716788
http://www.everything2.com/e2node/Decimal%2520IP%2520address
http://www.karenware.com/powertools/ptlookup.asp

2009Prof.Chin,ConsciousVibes.com

Lastsaved:3/6/201612:33PM

Page4of4