-2

I want to convert Japanese String to double byte format. Not sure how to do it. Have been stuck here since past 2 days.

I tried out some coding below but it didn't work . Japanese string is in sMessage

System.out.println(sMessage);

        String jMessage = new String(sMessage.getBytes(), "UTF-8");
        System.out.println(jMessage);


                sMessage.getBytes();

        System.out.println(sMessage);
puchu
  • 69
  • 1
  • 9
  • *"but it didn't work"* This isn't a proper problem description. Please read [how-to-ask](http://stackoverflow.com/help/how-to-ask). – Tom Feb 23 '16 at 19:02
  • Can you define "double byte format"? – fge Feb 23 '16 at 19:02
  • Without knowing how the string is encoded, nobody will be able to help you. – Frank Puffer Feb 23 '16 at 19:03
  • 1
    By "double byte", do you mean UTF-16? You have to know if you want `UTF-16LE` or `UTF-16BE` – Peter Lawrey Feb 23 '16 at 19:03
  • Have you already looked at http://stackoverflow.com/questions/24009119/utf-8-encoding-only-some-japanese-characters-are-not-getting-converted ? – PM 77-1 Feb 23 '16 at 19:06
  • And you want what exactly? This more and more sounds like an XY problem, so why don't you start by saying what you want to achieve? Also, a hint: **a `String` has no encoding**. – fge Feb 23 '16 at 19:06

1 Answers1

0

This is what I believe you want to do

sMessage.getBytes("UTF-8");

You need to define what charset should be used when converting to a byte array. Otherwise it will use the default, read from the environment variable

file.encoding

Note that UTF-8 does not mean "double byte format", it can be up to 4 bytes per character.

https://sv.wikipedia.org/wiki/UTF-8

gustf
  • 1,959
  • 13
  • 20