Click to See Complete Forum and Search --> : Character type problem


lukermsdn111
March 14th, 2010, 09:27 AM
String tempString = testData[5]; // testData[5] = 17896
char tempArray[] = tempString.toCharArray();

int groupAddress = tempArray[0] + tempArray[1] + tempArray[2] + tempArray[3];
int cacheIndex = tempArray[3];
int byteOffset = tempArray[4];
int cacheTag = tempArray[0] + tempArray[1] + tempArray[2];

System.out.println(groupAddress); // Prints 217
System.out.println(cacheIndex); // Prints 57
System.out.println(byteOffset); // Prints 54
System.out.println(cacheTag); // Prints 160

Here the Program (I think) is printing out ASCII characters for the numbers rather than the numbers themselves. How would i make the program print out the numbers and not the ASCII characters?

Luke

dlorde
March 15th, 2010, 06:06 AM
Two easy ways come to mind - you can convert a digit character to its numeric value with Character.digit(char), or you can convert a String number to a numeric value using Integer.parseInt(str, radix). So to get an array of ints from the String, you could do this:String tempString = "17896";

// use Character.digit(..)
char tempArray[] = tempString.toCharArray();
int[] valueArray = new int[tempArray.length];
for (int i=0; i < tempArray.length; i++) {
valueArray = Character.digit(tempArray[i], 10);
}

// alternatively, use Integer.parseInt(..)
int[] valueArray = new int[tempString.length()];
for (int i=0; i < tempString.length(); i++) {
valueArray[i] = Integer.parseInt(tempString.substring(i, i+1));
}[I]The problem is never how to get new, innovative thoughts into your mind, but how to get old ones out!
D. Hock