Im having problems whith base64 between java and c#, im sending encoded string to a asp.net handler from an java urlconnection, i compare both strings, the one generated in java from byte array and the one received in asp.net first decoding and both are identical but after decode the byte array in c# arent equal from byte array in java.
Im using new sun.misc.BASE64Encoder().encode(javabytearray); in java and System.Convert.FromBase64String(encodedstring); in dotnet.
from java: "[0] [-24] [56] [1] [-56] [41] [-29] ........."
to dotnet: "[0] [232] [56] [1] [200] [41] [227] ........."
Similar: Encoding base64 in Java and decoding in C#
i cant ask it in question above because it is an Q&A site and every time an new question must be created, cant ask an question inside another question.
thanks a lot
Asked
Active
Viewed 6,821 times
2
-
2Seems like bytes in -128..0 range are converted to 256+b in c#. Loss of sign somewhere. – Victor Sorokin Apr 27 '11 at 13:44
-
Mate - thanks for this post. – AntDC Jul 18 '18 at 14:17
1 Answers
9
byte
is unsigned in C# and signed in Java. The bit pattern of the Java byte
value -24
is equal to the bit pattern of the c# byte
value 232
. So your code should be correct. If you want to verify this, convert e.g. the Java byte
values to int
and add 256
to the negative values.

Aasmund Eldhuset
- 37,289
- 4
- 68
- 81
-
To expand on Aasmund's answer all you have to do is convert the result of FromBase64String to an unsigned byte[]. You could also just write your own method that gives you an unsigned byte result. – Security Hound Apr 27 '11 at 13:43
-
@Ramhound: Good point, except that I guess it must be done the other way (since the C# bytes are already unsigned) - the `byte[]` can be converted to `sbyte[]`. – Aasmund Eldhuset Apr 27 '11 at 13:50
-
Hi. thanks a lot for so fast answer, i have a third-party biometric software that have {byte[] buffer = t.serialize()} , {t.unserialize(buffer)} and {t.unserialize(stream)} in both sides, cant pass signed byte array in dotnet side. – hee Apr 27 '11 at 14:50