My goal here is to take a dotted notation IPv6 number and convert it to colon he
ID: 3543018 • Letter: M
Question
My goal here is to take a dotted notation IPv6 number and convert it to colon hex notation and colon hex notation with zero compression. I have completed the coversion to colon hex notation but am having an issue trying to figure out a way to perfor the zero compression on the number. The complete code is here with notes and comments and will compile properly and output a colon hex notation number given a 16 bit IPv6 number, now as stated I just need to write another method to perform zero compression and print that out as well. Zero compression is when you have two or more 0's back to back in the colon hex notation, you can replace them with just two colons. See the example below:
Ex. Colon hex number: FF69:0:0:0:0:0:0:FF56
Ex. Zero compression: FF69::FF56
Code:
/*
* Name: Phillip Vinson
* Date: 09/29/2013
* Question: Below
- Page 417
- question 24.9 and question 24.10
- input: a 128-bit number written in the dotted decimal notation.
- Output:
- the number in colon hex notation.
- and
- the number in colon hex notation with zero compression.
*/
package colonhex;
import java.util.Scanner;
import java.util.ArrayList;
public class ColonHex
{
//method to get hexadecimal number
private static String getHexadecimal(int x)
{
int hexNum = x;
double hexNumVal = x;
String val = null;
boolean end = false;
double hexDoubleNum = 0;
//array to hold new hex values
ArrayList<String> valArray = new ArrayList<String>();
//start do loop to begin conversions from decimal to hex
do
{
//perform when the quotient is 0
if(hexNum == 0)
{
end = true;
//if the quotient size starts as 0, meaning a zero was passed do the following
if(valArray.size() == 0)
{
String value = "0";
valArray.add(value);
}
}
else
{
hexDoubleNum = hexNum/16.0; //returns the quotient
double remainder = hexDoubleNum - (int)hexDoubleNum; //get the remainder
hexNumVal = remainder*16; //returns the hex number
//If the hex number is over 10 change to the proper A, B, C, D, E, F
if(hexNumVal >= 10)
{
switch((int)hexNumVal)
{
case 10: val = "A";
break;
case 11: val = "B";
break;
case 12: val = "C";
break;
case 13: val = "D";
break;
case 14: val = "E";
break;
case 15: val = "F";
break;
}
}
//if hex is under 10 convert to a string and store in val variable
else
{
val = Integer.toString((int)hexNumVal);
}
//add val variable to arrayList
valArray.add(val);
}
//convert the hexDoubleNum to integer format for restart of while loop
hexNum = (int)hexDoubleNum;
}while(end != true); //exit loop when end equals true
//create a new String array the same size as the size of the valArray arrayList
int arraySize = valArray.size();
int z = arraySize;
String[] newArray = new String[z];
int y =0;
//Store all the arrayList values in a new String array
while(z != 0)
{
String storedVal = valArray.get(z-1);
newArray[y] = storedVal;
z--;
y++;
}
//store all the new hex numbers in a single String variable for returning the hex number
String hexNumReturn = "";
for(int r = 0; r < newArray.length; r++)
{
hexNumReturn = hexNumReturn + newArray[r];
}
//Trim the new string to ensure there are no extra spaces anywhere
String trimNum = hexNumReturn.trim();
//return the new variable containing the hex number to the method
return trimNum;
}
//THIS METHOD IS UNDER CONSTRUCTION TO PERFORM THE FOLLOWING:
//method to perform zero compression on a dotted colon notation entry
private static String zeroCompression(String num)
{
String zeroCompNum = null;
//store the string into a new array splitting all the ":" instances
String zeroSplit[] = new String[16];
zeroSplit = zeroCompNum.split(":");
//Run through the entire array, if back to back 0's are found replace with
//a double colon
//EX: you get FF0C:0:0:0:0:160
//can be written: FF0C::160
//All the 0's are now gone
for(int sub = 0; sub < zeroSplit.length; sub++)
{
if(zeroSplit[sub] == "0" && zeroSplit[sub + 1] == "0")
{
}
}
return zeroCompNum;
}
//Main method
public static void main(String[] args)
{
//get user input
Scanner kb = new Scanner(System.in);
System.out.println("Enter a 16 bit dotted notation number: ");
String userString = kb.nextLine();
String stringVal[] = new String[16];
//Store user input in String array and split at "."
stringVal = userString.split("\.");
//convert String array to Integer Array
int[] userVal = new int[16];
for (int x=0; x<16; x++)
{
userVal[x] = Integer.parseInt(stringVal[x]);
}
//call method to get hexadecimal notation and store in a new String
String[] hexNum = new String[16];
for(int x=0; x<16; x++)
{
hexNum[x] = getHexadecimal(userVal[x]);
}
//Set up the Colon Hex Notation
String hexNumDotted = hexNum[0] + ":" + hexNum[1] + ":" + hexNum[2] +
":" + hexNum[3] + ":"+ hexNum[4] + ":" + hexNum[5] + ":" + hexNum[6] +
":" + hexNum[7] + ":"+ hexNum[8] + ":" + hexNum[9] + ":" + hexNum[10] +
":" + hexNum[11] + ":"+ hexNum[12] + ":" + hexNum[13] + ":" + hexNum[14] +
":" + hexNum[15];
//Print out the Colon Hex Notation of a number
System.out.println(" Hex Number Colon Notation: " + hexNumDotted);
//cal the zeroCompression method and pass a String variable in Colon Hex Notation
String zeroCompressionNumber = zeroCompression(hexNumDotted);
//Print out the zero compressed colon hex notation of the above passed argument
System.out.println(" Hex Number Colon Notation with Zero Compresson: " + zeroCompressionNumber);
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.ArrayList;
public class ColonHex
{
//method to get hexadecimal number
private static String getHexadecimal(int x)
{
int hexNum = x;
double hexNumVal = x;
String val = null;
boolean end = false;
double hexDoubleNum = 0;
//array to hold new hex values
ArrayList<String> valArray = new ArrayList<String>();
//start do loop to begin conversions from decimal to hex
do
{
//perform when the quotient is 0
if(hexNum == 0)
{
end = true;
//if the quotient size starts as 0, meaning a zero was passed do the following
if(valArray.size() == 0)
{
String value = "0";
valArray.add(value);
}
}
else
{
hexDoubleNum = hexNum/16.0; //returns the quotient
double remainder = hexDoubleNum - (int)hexDoubleNum; //get the remainder
hexNumVal = remainder*16; //returns the hex number
//If the hex number is over 10 change to the proper A, B, C, D, E, F
if(hexNumVal >= 10)
{
switch((int)hexNumVal)
{
case 10: val = "A";
break;
case 11: val = "B";
break;
case 12: val = "C";
break;
case 13: val = "D";
break;
case 14: val = "E";
break;
case 15: val = "F";
break;
}
}
//if hex is under 10 convert to a string and store in val variable
else
{
val = Integer.toString((int)hexNumVal);
}
//add val variable to arrayList
valArray.add(val);
}
//convert the hexDoubleNum to integer format for restart of while loop
hexNum = (int)hexDoubleNum;
}while(end != true); //exit loop when end equals true
//create a new String array the same size as the size of the valArray arrayList
int arraySize = valArray.size();
int z = arraySize;
String[] newArray = new String[z];
int y =0;
//Store all the arrayList values in a new String array
while(z != 0)
{
String storedVal = valArray.get(z-1);
newArray[y] = storedVal;
z--;
y++;
}
//store all the new hex numbers in a single String variable for returning the hex number
String hexNumReturn = "";
for(int r = 0; r < newArray.length; r++)
{
hexNumReturn = hexNumReturn + newArray[r];
}
//Trim the new string to ensure there are no extra spaces anywhere
String trimNum = hexNumReturn.trim();
//return the new variable containing the hex number to the method
return trimNum;
}
//THIS METHOD IS UNDER CONSTRUCTION TO PERFORM THE FOLLOWING:
//method to perform zero compression on a dotted colon notation entry
private static String zeroCompression(String num)
{
String zeroCompNum="";
//store the string into a new array splitting all the ":" instances
String zeroSplit[] = new String[16];
zeroSplit = num.split(":");
//Run through the entire array, if back to back 0's are found replace with
//a double colon
//EX: you get FF0C:0:0:0:0:160
//can be written: FF0C::160
//All the 0's are now gone
for(int sub = 0; sub < zeroSplit.length; sub++)
{
if(!zeroSplit[sub].equalsIgnoreCase("0"))// && zeroSplit[sub + 1] == "0")
{
zeroCompNum = zeroCompNum + zeroSplit[sub];
//if(sub!=zeroSplit.length-1)
// zeroCompNum = zeroCompNum + "::";
}
else
{
if(sub<zeroSplit.length-1 && !zeroSplit[sub+1].equalsIgnoreCase("0"))
zeroCompNum = zeroCompNum + "::";
}
}
return zeroCompNum;
}
//Main method
public static void main(String[] args)
{
//get user input
Scanner kb = new Scanner(System.in);
System.out.println("Enter a 16 bit dotted notation number: ");
String userString = kb.nextLine();
String stringVal[] = new String[16];
//Store user input in String array and split at "."
stringVal = userString.split("\.");
//convert String array to Integer Array
int[] userVal = new int[16];
for (int x=0; x<16; x++)
{
userVal[x] = Integer.parseInt(stringVal[x]);
}
//call method to get hexadecimal notation and store in a new String
String[] hexNum = new String[16];
for(int x=0; x<16; x++)
{
hexNum[x] = getHexadecimal(userVal[x]);
}
//Set up the Colon Hex Notation
String hexNumDotted = hexNum[0] + ":" + hexNum[1] + ":" + hexNum[2] +
":" + hexNum[3] + ":"+ hexNum[4] + ":" + hexNum[5] + ":" + hexNum[6] +
":" + hexNum[7] + ":"+ hexNum[8] + ":" + hexNum[9] + ":" + hexNum[10] +
":" + hexNum[11] + ":"+ hexNum[12] + ":" + hexNum[13] + ":" + hexNum[14] +
":" + hexNum[15];
//Print out the Colon Hex Notation of a number
System.out.println(" Hex Number Colon Notation: " + hexNumDotted);
//cal the zeroCompression method and pass a String variable in Colon Hex Notation
String zeroCompressionNumber = zeroCompression(hexNumDotted);
//Print out the zero compressed colon hex notation of the above passed argument
System.out.println(" Hex Number Colon Notation with Zero Compresson: " + zeroCompressionNumber);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.