0

I have converted UTF 16 to UTF 8 (method used : toUTF8Array)codes in javascript and sent the code from ajax call(note : UTF 16 chars is ugly and it needed encoding which I dont want!) Js code :

var compressed = LZString.compressToUTF16(uncompressed64Data);
var utf8values = toUTF8Array(compressed); 
    jQuery.ajax({
data :"img=+" utf8values 
}

I have received the UTF 8 codes in java server side as follow :

 String utf8values = request.getParameter("img");

fyi, outcome of sop: utf8values : 225,186,162,227,160,181,229,160,165

Now I want change the codes into actual UTF 16 string?

Thanks in advance!

11
  • Is "225,186,162,227,160,181,229,160,165" The actual string you have? Commented Sep 22, 2017 at 9:13
  • Yes . This is sample I given Commented Sep 22, 2017 at 9:15
  • So it's supposed to be Ả㠵堥 ? Commented Sep 22, 2017 at 9:23
  • The attribute is named img, are you trying to send image data to the backend? Commented Sep 22, 2017 at 9:45
  • @Oleg : exactly!! By the way are we able to send UTF 16 chars (Ả㠵堥) without encoding into server.? currently I have huge UTF16 data which is not going to server due to non encoding!! Commented Sep 22, 2017 at 9:50

1 Answer 1

1

The following should work:

String[] split = "225,186,162,227,160,181,229,160,165".split(",");
ByteBuffer bb = ByteBuffer.allocate(split.length);
Arrays.stream(split).forEach(a -> bb.put((byte)Integer.parseInt(a)));
bb.flip();
String string = Charset.forName("UTF-8").decode(bb).toString();
Sign up to request clarification or add additional context in comments.

2 Comments

excellent answer. I would be more happy if you able to extract this java 1.7. Thanks
@WillMcavoy I just used it for some brevity replacing forEach with a loop is easy enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.