How to convert sbyte [] to base64 string?

How to convert string sbyte[]to base64?

I cannot convert this sbyte[]to byte[]to maintain compatibility with java.

+5
source share
2 answers

You absolutely can convert sbyte[]to byte[]- I can pretty much guarantee you that the Java code will really treat the byte array as unsigned. (Or, to put it another way: base64 is only defined using unsigned bytes ...)

byte[] Convert.ToBase64String. byte[] - # , CLR , #:

sbyte[] x = { -1, 1 };
byte[] y = (byte[]) (object) x;
Console.WriteLine(Convert.ToBase64String(y));

byte[], :

byte[] y = new byte[x.Length];
Buffer.BlockCopy(x, 0, y, 0, y.Length);

.

+11
class Program
{
    static void Main()
    {
        sbyte[] signedByteArray = { -2, -1, 0, 1, 2 };
        byte[] unsignedByteArray = (byte[])(Array)signedByteArray; 
        Console.WriteLine(Convert.ToBase64String(unsignedByteArray));
    }
}
+5

All Articles