|
-
March 14th, 2010, 09:27 AM
#1
Character type problem
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
-
March 15th, 2010, 06:06 AM
#2
Re: Character type problem
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:
Code:
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[i] = 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));
}
The problem is never how to get new, innovative thoughts into your mind, but how to get old ones out!
D. Hock
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|