I am struggling with different results when converting a string to bytes in C# vs. Java.
C#:
byte[] byteArray = Encoding.Unicode.GetBytes ("chess ¾");
for (int i = 0; i < byteArray.Length; i++)
System.Diagnostics.Debug.Write (" " + byteArray[i]);
System.Diagnostics.Debug.WriteLine("");
System.Diagnostics.Debug.WriteLine(Encoding.Unicode.GetString(byteArray));
displays:
99 0 104 0 101 0 115 0 115 0 32 0 190 0
chess ¾
Java:
byte[] byteArray = "chess ¾".getBytes("UTF-16LE");
for (int i = 0; i < byteArray.length; i++)
System.out.print(" " + (byteArray[i]<0?(-byteArray[i]+128):byteArray[i]));
System.out.println("");
System.out.println(new String(byteAppName,"UTF-16LE"));
displays:
99 0 104 0 101 0 115 0 115 0 32 0 194 0
chess ¾
Notice that the second to last value in the byte array is different! My objective is to encrypt this data and be able to read it from either C# or Java. This difference appears to be an obstacle.
As a side note, before I learned to use Unicode(C#)/UTF-16LE(Java), I was using UTF-8 ...
C#: byte[] byteArray = Encoding.UTF8.GetBytes ("chess ¾");
displays: 99 104 101 115 115 32 194 190
Java: byteArray = appName.getBytes("UTF-8");
displays: 99 104 101 115 115 32 190 194
Which, strangely results in the second to last and last bytes being flipped.
Lastly, Unicode for ¾ is decimal 190 (http://www.fileformat.info/info/unicode/char/BE/index.htm), not decimal 194 (Â) (http://www.fileformat.info/info/unicode/char/00c2/index.htm).
Any help would be greatly appreciated.
byteArray = new byte[] {99, 0, 104, 0, 101, 0, 115, 0, 115, 0, 32, 0, -62, 0};displays:99 0 104 0 101 0 115 0 115 0 32 0 190 0chess Âbyte[] byteArray = new byte[] {99, 0, 104, 0, 101, 0, 115, 0, 115, 0, 32, 0, 194, 0};displays:99 0 104 0 101 0 115 0 115 0 32 0 194 0chess Â