0

I might be somewhat stupid here, but I can't seem to think of a straightforward solution to this problem.

I've currently got an int[] that contains ASCII character codes, however, with the ASCII table, any value < 32 is a control code. So what I need to do is for any value > 32, put the ASCII character into a char[], however if it's < 32, just put the literal integer value in as a character.

For example:

public static void main(String[] args) {
    int[] input = {57, 4, 31}; //57 is the only valid ASCII character '9'
    char[] output = new char[3];

    for (int i = 0; i < input.length; i++) {
        if (input[i] < 32) { //If it's a control code
            System.out.println("pos " + i + " Not an ascii symbol, it's a control code");
            output[i] = (char) input[i];
        } else { //If it's an actual ASCII character
            System.out.println("pos " + i + " Ascii character, add to array");
            output[i] = (char) input[i];
        }
    }

    System.out.println("\nOutput buffer contains:");
    for (int i = 0; i < output.length; i++) {
        System.out.println(output[i]);

    }
}

Output is:

pos 0 Ascii character, add to array
pos 1 Not an ascii symbol, it's a control code
pos 2 Not an ascii symbol, it's a control code

Output buffer contains:
9 // int value 57, this is OK

As you can see the last two entries in the array are blank, as there isn't actually an ASCII character for either 4, or 31. I know there are methods for converting Strings to char[], however what's the general idea when you've already got a char[] in which you want the value.

There is probably a really easy solution for this, I think I'm just having a dumb moment!

Any advice would be appreciate, thanks!


4
  • Maybe the characters are printed blank? Commented May 17, 2013 at 12:49
  • Well the problem is that any int value in the ASCII table < 31 isn't an alphanumeric character, in that case I just want the literal value, e.g. '4' and '31'. Commented May 17, 2013 at 12:53
  • They actually are printed. The problem is that some of those control characters represented as int may have two digits, so they probably won't be chars... Commented May 17, 2013 at 12:53
  • Ahh yes of course, 31 isn't going to be a single character is it it's going to be '3' and '1'. Good point, I don't think a char[] is the right way to go about doing this. Thanks Mateusz. Commented May 17, 2013 at 12:55

2 Answers 2

1

For classifying characters you should use the Character.getType(char) method.

To store either a character or an integer you could try using a wrapper object to do that.

Alternatively you could wrap your char like this:

static class NiceCharacter {
  // The actual character.
  final char ch;

  public NiceCharacter ( char ch ) {
    this.ch = ch;
  }

  @Override
  public String toString () {
    return stringValue(ch);
  }

  public static String stringValue ( char ch ) {
    switch ( Character.getType(ch)) {
      // See http://en.wikipedia.org/wiki/Mapping_of_Unicode_characters for what the Cc group is.
      // See http://en.wikipedia.org/wiki/Control_character for a definition of what  are CONTROL characters.
      case Character.CONTROL:
        return Integer.toString(ch);

      default:
        return Character.toString(ch);
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change how you print the output buffer

for (int i = 0; i < output.length; i++) {
    if (output[i] < 32){
        System.out.println("'" + (int)output[i] + "'"); //Control code is casted to int.
        //I added the ' ' arround the value to know its a control character
    }else {
        System.out.println(output[i]); //Print the character
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.