Java int array for StringBuilder

How to convert int array with UTF-8 string to StringBuilder in while loop? For instance:
int array: 71, 73, 70, 56, 57, 97, 149, 0, 55, 0, 247 ...
result line: GIF89a β€’ € Γ· € € €€ Γ€ΓœΓ€ | Êð *? Βͺ *? ΓΏ ...
The line contains latin, cyrillic and asian characters, as well as various characters and numbers

do buffer.append((char)num[++i]); while((byte)buffer.charAt(buffer.length()-1) != -1); 

This method breaks all non-Latin characters.

+4
source share
2 answers

First of all, convert int [] to byte [] as follows:

  //intArray contains your data... byte[] utf8bytes = new byte[intArray.length]; for(int i = 0; i < intArray.length; i++) { utf8bytes[i] = (byte) intArray[i]; } 

Then create a string from your bytes that defines UTF-8 as an encoding:

  String asString = new String(utf8bytes, "UTF-8"); 
+3
source

You read in the GIF89a file as a unit for each byte, and then print it as if it were a text string. The main problem is that the integers (bytes) inside this file are not actually mapped to significant text characters, so when the display cannot display parts of the alphabet, it will display everything that your text encodings dictate (which seems to me very a lot of trash).

Graphic information does not always correctly display text. Although there are 256 possible byte values, and sometimes one or more bytes will be a single character, there are only 26 letters in the English alphabet that are presented in upper and lower case. Along with ten digits and several punctuation, you get about 80 different characters that are widely used in the essay. The remaining 160 characters are control codes, signals for using several bytes or matching with the characters present to support the display of foreign languages.

This garbage is closest to the valid bytes for character matching for your current character set. If you want the best result, try reading a file that contains data that maps to something related to a character.

0
source

Source: https://habr.com/ru/post/1416672/


All Articles