How to convert a sbyte[] to byte[] in C#?

前端 未结 3 1435
情话喂你
情话喂你 2020-12-08 19:22

I\'ve got a function which fills an array of type sbyte[], and I need to pass this array to another function which accepts a parameter of type byte[].

Can I convert

3条回答
  •  半阙折子戏
    2020-12-08 20:15

    Yes, you can. Since both byte and sbyte have the same binary representation there's no need to copy the data. Just do a cast to Array, then cast it to byte[] and it'll be enough.

    sbyte[] signed = { -2, -1, 0, 1, 2 };
    byte[] unsigned = (byte[]) (Array)signed; 
    

提交回复
热议问题