1

I have the following text:

Анна Меркулова

With help of the following online decoder https://2cyr.com/decode/?lang=en I was able to decode mentioned string to the correct one:

Анна Меркулова

enter image description here

Source encoding is UTF-8 and the target is WINDOWS-1251

but I still unable to do it programmatically in Java:

String utf8String = new String("Анна Меркулова".getBytes(), "UTF-8");
String ansiString = new String(utf8String.getBytes("UTF-8"), "windows-1251");
System.out.println(ansiString);

returns

Анна Меркулова

What am I doing wrong and how to properly convert the string?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • String in Java uses internally always the same encoding. new String() creates one String from the encoding you set. If you want to output another encoding you must open an outputstream where you set the encoding. You could use an OutputStreamWriter. – mayamar Mar 03 '19 at 08:47

1 Answers1

3

You're trying to assign the String(s) a Charset, but what you really need to do is extract the bytes with a specific Charset

final byte[] bytes = "Анна Меркулова".getBytes("UTF-8");
final String utf8String = new String(bytes);
final byte[] bytes1 = utf8String.getBytes("windows-1251");
final String ansiString = new String(bytes1);

And by the way, you don't need all of that

final byte[] bytes = "Анна Меркулова".getBytes("windows-1251");
final String result = new String(bytes);
LppEdd
  • 20,274
  • 11
  • 84
  • 139