2

I have a Byte array and each byte in the array corresponds to an ASII character(8 bit ASCII character).I am trying the get the whole list of ASII chars from the list.

byte[] data;

ArrayList<Character> qualAr = new ArrayList<>();
for (int i = 0; i < data.length; i++) {
    qualAr.add((char)data[i]);
}

The above method,did not print the all ASCII chars properly as many of the chars that was printed contained square boxes and empty space.If the issue is not setting the encoding,then how to set the type of encoding to ASCII in the above method? Most of the examples i saw where of UTF-8.

Update: Thank you all. The problem was not with the encoding. I had found new documentation stating that the values needs to converted using - ASCII+33 and without that the values tried to print the initial ASCII chars which wouldn't make any sense.

3
  • I don't think you can simply cast each byte to a char the way you are doing it above. Commented Aug 20, 2015 at 8:42
  • There is no such thing as 8-bit ASCII. What encoding are your data in? Commented Aug 20, 2015 at 10:35
  • The datatype mentioned as ASCII and description states 8-bit ASCII character Commented Aug 20, 2015 at 11:13

1 Answer 1

1

Try using the following code:

String dataConverted = new String(data, "UTF-8");
ArrayList<Character> qualAr = new ArrayList<>();
for (char c : dataConverted.toCharArray()) {
    qualAr.add(c);
}

I convert your byte array to a String, and then generate the list of characters. ASCII characters should be represented as one byte codes in UTF-8.

Keep in mind that the first 32 or so ASCII characters may render as boxes or blank spaces.

Here is a link to the basic ASCII table.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes there are boxes/space in first few chars and last few chars. Is it normal? The number of elements contained by the byte array is already given and because of the space and boxes the number of chars decoded from the byte array are not equal. Does this mean the encoding was not done properly?
How can the length of your data and qualAr ArrayList be different? If you give us more information about your input data maybe we can clarify your problem. In any case, my code should work if you really only have ASCII characters.
The length of qualAr and data are the same. But the qualar is supposed to have to be filled with only ASII chars not boxes or spaces. The data i have is of AB1 file format(used by sequencers) . The description for this specific char array mentions it contains -8 bit ASCII character or signed 8-bit integer.
@TimBiegeleisen The website you linked to refers to Windows-1252 as "Extended ASCII". Please do not link to factually incorrect sources.
@KarolS I provided a link in my answer to the basic ASCII table, thanks for pointing this out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.