CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2010
    Location
    Newcastle upon Tyne, UK
    Posts
    5

    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

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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 &#91;CODE]...your code here...&#91;/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
  •  





Click Here to Expand Forum to Full Width

Featured