1

I have a function which is returning byte array in both C++ and Java, the logic of the function is same.

Given that the byte array which is returned is same, when I print the array after converting to a basic string like:

std::string str(byteArray,byteArray+len)

I am able to see the output properly, but when I do something like:

new String(byteArray,"UTF-8")

I get some unknown characters on the terminal. How to retrieve the same output as that of C++?

13
  • 1
    Some code may actually help. Commented Apr 3, 2018 at 10:27
  • @AleksandrMukhalov i just want to know if there is a statement which is equivalent to std::string str(byteArray,byteArray+len) in java Commented Apr 3, 2018 at 10:30
  • Are you sure UTF-8 is the right encoding, and the c++ constructor uses that? Commented Apr 3, 2018 at 10:30
  • 1
    @kumarD Well what is your byte array? What is the source string? Commented Apr 3, 2018 at 10:32
  • 1
    Have you confirmed that the byte array is identical after decoding in C++ vs decoding in Java? Commented Apr 3, 2018 at 10:51

3 Answers 3

2

Here's the problem. When you do this:

    new String(byteArray,"UTF-8")

you are saying to the runtime system this:

The byte array contains character data that has been encoded as UTF-8. Convert it into a sequence of Unicode codepoints1 and give them to me as a Java String.

But the bytes in the byte array are clearly NOT a well-formed UTF-8 sequence, because you are getting stuff that looks like garbage.

So what is going on? Well I think that there are two possibilities:

  1. The bytes in the array could actually be characters in a different character encoding. It is clearly not ASCII data because pure 7-bit ASCII is also well-formed as UTF-8. But the bytes could be encoded in some other character encoding. (If we actually had the byte values, we might be able to make an educated guess as to which encoding was used.)

  2. The bytes in the array could actually be garbled. You say that they were obtained by decrypting AES encrypted data. But if you somehow got the decryption incorrect (e.g. you used the wrong key), then you would end up with garbled stuff.

Finally, the closest equivalent in Java to std::string str(byteArray,byteArray+len) is this:

  new String(byteArray, "LATIN-1")

This is because each encoded byte in an LATIN-1 sequence is equal in value to the equivalent Unicode code point.

Whether it is unclear whether that would actually work in your case. Certainly, it won't work if the bytes were garbled due to an incorrect encryption or decryption. Or garbling of the encrypted data in transmission.


1 - actually, UTF-16 code units ... but that's another story.

Sign up to request clarification or add additional context in comments.

Comments

0

In java I convert byte array like below. This "UTF-8" might create a problem in your case.

new String(byteArray);

Also try with

 new String(byteArray,"UTF-16");

If both the above does not work you can try with below:-

 UnicodeEncoding uEncoding = new UnicodeEncoding();
 string stringContent=uEncoding.GetString(byteArray);

also for detail read http://www.oracle.com/us/technologies/java/supplementary-142654.html

5 Comments

This isn't working for me, hence the existence of the question, i am facing encoding issues like. .�[~:D�}��m�,DZ1����U`�]'���5kKx�E����o�W�tw& �HK�"(<D�>e�{�"�����W|�A���r�"��;Õ}��9=�sT�7��v��rA}��a�4n#���h��m��PYn��V�R�fS��� ���x!�s�p�IU����Xĩۨ���Y�I̫�ޥ.��
I think you need to try with all the character set. Better contact the source team in what format they are sending the information and decode accordingly.
it works very well with c++, as you can see in my question. Facing problems in java.
Which library is that function from?
0

So, here goes the solution, the problem here was the decryption wasn't properly going through, it wasn't complete but partial, hence there were characters which we could make sense of and the rest were junk, the blunder which i did was using SHA-512 as the message digest algorithm while encryption and MD-5 while decryption.

Cheers!!

Comments

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.