ここで私はややばかげているかもしれませんが、この問題に対する簡単な解決策を思いつかないようです。
現在、ASCII 文字コードを含む int[] を取得していますが、ASCII テーブルでは、32 未満の値はすべて制御コードです。したがって、32を超える値の場合はASCII文字をchar []に入れる必要がありますが、32未満の場合は、リテラル整数値を文字として入れるだけです。
例えば:
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]);
}
}
出力は次のとおりです。
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
Strings
ご覧のとおり、配列の最後の 2 つのエントリは空白です。これは、実際には 4 または 31 の ASCII 文字が存在しないためchar[]
です。値が必要な char[]。
これにはおそらく本当に簡単な解決策があります。私はただ愚かな瞬間を過ごしていると思います!
アドバイスをいただければ幸いです。