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

前端 未结 3 1436
情话喂你
情话喂你 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:13

    If you are using .NET 3.5+, you can use the following:

    byte[] dest = Array.ConvertAll(sbyteArray, (a) => (byte)a);
    

    Which is, I guess effectively copying all the data.

    Note this function is also in .NET 2.0, but you'd have to use an anonymous method instead.

提交回复
热议问题