如何转换为sbyte [],在C#中的byte []? [英] How to convert a sbyte[] to byte[] in C#?

查看:319
本文介绍了如何转换为sbyte [],在C#中的byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到填补类型为sbyte []数组的功能,我需要这个数组传递给它接受类型为byte []的参数另一个函数。

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 it nicely and quickly, without copying all the data or using unsafe magic?

推荐答案

是的,可以。
由于两个字节为sbyte具有相同的二进制重新presentation没有必要复制数据。
只要做强制转换为数组,然后将它转换为byte [],它会是不够的。

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; 

这篇关于如何转换为sbyte [],在C#中的byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆