How to convert a string encoded in windows-1250 / Cp1250 to utf-8?

As the title says ... I read the content from htto's answer

 

    InputStream is = response.getEntity().getContent();
    String cw = IOUtils.toString(is);
    byte[] b = cw.getBytes("Cp1250");
    String x = StringUtils.newStringUtf8(b);
    String content = new String(b, "UTF-8");

    System.out.println(content);

 

I have tried many options. I am a little confused about which correct coding constants are used as strings. windows-1250 or cp1250. UTF-8 or utf-8 or utf8?

+5
source share
5 answers

It seems you think the object Stringis encoded. It is not right. Encoding is used as part of the conversion from binary data (a byte[]or InputStream) to text data (a Stringor char[]etc.).

, IOUtils.toString, , , . Windows-1250, InputStreamReader InputStream, InputStreamReader.

, UTF-8 - UTF-8 , byte[], .

+6

. byte, String(byteArray, "Cp1250") String. , UTF-8, String.getBytes("UTF-8").

+4

() . , "UTF-8" - , java "UTF8"; . "Windows-1250", HTML. "Cp1250" ( ) java.

java [] - , String ( Unicode) - . , , .

InputStream, OutputStream ↔ String, char, Reader, Writer

String cw = IOUtils.toString(is, "UTF-8"); // InputStream is binary gives byte[], hence give encoding
byte[] b = cw.getBytes("Cp1250");
String x = new String(b, "Cp1250");
String content = s;

System.out.println(content);

(qua encoding) String, String char, UTF-16. .class UTF-8 ( ).

+3

, Apo Commons IO , :

String cw = IOUtils.toString(is, "windows-1250");

UTF-16 Java. .

0

Scanner .

    FileInputStream is = new FileInputStream(fileOrPath);
    Scanner scanner = new Scanner(is, "cp1250");
    String out = scanner.next();

next() String .

" " "cp1250" "UTF-8".

-1