2
votes


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

1
Seems like bytes in -128..0 range are converted to 256+b in c#. Loss of sign somewhere.Victor Sorokin
Mate - thanks for this post.AntDC

1 Answers

9
votes

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.