2

I have two byte array from( in C# and Java ) a string. When I convert arrays, the results do not match. What could be the reason for this? This problem is not constant. Sometimes it produces the same results.

 C#:
[0] 148 
[1] 70  
[2] 38  
[3] 173 
[4] 249 
[5] 227 
[6] 183 
[7] 106 
[8] 57  
[9] 25  
[10] 181    
[11] 13 
[12] 192    
[13] 176    
[14] 128    
[15] 164    

   Java :


    0 = -108 
    1 = 70
    2 = 38
    3 = -83
    4 = -7
    5 = -29
    6 = -73
    7 = 106
    8 = 57
    9 = 25
    10 = -75
    11 = 13
    12 = -64
    13 = -80
    14 = -128
    15 = -92

C# result : �F&���j9�\r����

Java Result : �F&���j9�0��

Edit :

Converter Code;

C# -
String result = UTF8Encoding.UTF8.GetString(byteArray);

Java: 
String result = new String(byteArray, "UTF-8");

*Edit-2 : Its true convert.

C# :
        [0] 239 
        [1] 195 
        [2] 40  
        [3] 19  
        [4] 185 
        [5] 36  
        [6] 77  
        [7] 132 
        [8] 182 
        [9] 122 
        [11]    173 
        [12]    12  
        [13]    191 
        [14]    100 
        [15]    118 
Java :

0 = -17
1 = -61
2 = 40
3 = 19
4 = -71
5 = 36
6 = 77
7 = -124
8 = -74
9 = 122
10 = -70
11 = -83
12 = 12
13 = -65
14 = 100
15 = 118

C# result : ��(�$M��z���dv Java result : ��(�$M��z���dv*

5
  • Perhaps some code might help ... ? Commented Jul 11, 2017 at 6:33
  • 2
    Those are the same values, it just seem that you display them in a different format. They are unsigned in C# (range 0 / 255) and signed in Java (range -128 / +127) Commented Jul 11, 2017 at 6:34
  • Please provide a minimal reproducible example which shows the UTF-16 code units of the resulting strings. Also indicate where the bytes came from. If they weren't UTF-8-encoded text to start with, you shouldn't be trying to decode them as UTF-8. Commented Jul 11, 2017 at 7:02
  • @JonSkeet java : value.getBytes(Charset.forName("UTF-8")); C# : UTF8Encoding encoder = new UTF8Encoding(); encoder.GetBytes(value); .. and I can not change C# code. Its in server Commented Jul 11, 2017 at 7:06
  • So you should know what the original string is, and you should be able to say which decoding is going wrong. Please provide a minimal reproducible example, which will make it much easier to help you. Commented Jul 11, 2017 at 7:15

2 Answers 2

3

I'm assuming the outputs you displayed above are the byte arrays corresponding with the String in C# and Java.

byte in Java is a signed type (between -128 and 127), which explains the negative values for some of the bytes.

It looks like all the values that are positive for Java match the corresponding C# values, and only the negative values in Java don't match the corresponding C# values. The reason for that is that C# bytes are unsigned (between 0 and 255).

If you print the unsigned values corresponding with the Java bytes, you should get the same output as in C#:

for (byte b : byteArray)
    System.out.println(b & 0xff);
Sign up to request clarification or add additional context in comments.

6 Comments

OK, but for some arrays (Java: signed, C #: unsigned) it can produce the same results in both.
@fatihbolat Of course, if all the bytes happen to be between 0 and 127, both Java and C# would produce the same output.
stackoverflow.com/a/45027202/8287517 @eran. @H B is right. So I expect to produce the same strings. However, this is not the case in some cases.
@fatihbolat How did you produce these byte arrays?
java : value.getBytes(Charset.forName("UTF-8")); C# : UTF8Encoding encoder = new UTF8Encoding(); encoder.GetBytes(value); @Eran
|
1

Binary value for -108 and 148 both are same.

In java if you convert string to byte array then value of char grater then 127 is converted to negative value.

Look at value of 148 the windows calculator :

enter image description here

And same value for -108 :

enter image description here

Now discard the bits after first 8 bit and that is 1001 0100 which is same in both. Note : Need to discard, because length of byte is 8 bit.

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.