转换的byte []到sbyte [] [英] Convert byte[] to sbyte[]

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

问题描述

我试图数组从转换的byte [] 为sbyte []



下面是我的样品阵列:

 字节[]符号= {为0x00,0xFF的,为0x1F ,0x8F,0x80的}; 



我已经尝试过这样的:

 为sbyte [] =签署(为sbyte [])((阵列)的无符号); 



不过,这是行不通的。有此操作后,在数组中没有价值。



有谁有一个更好的主意?


解决方案

 为sbyte [] =签署(为sbyte [])(阵列)符号; 

此工作,因为字节和为sbyte具有在存储器中的相同的长度,并且可以无需改变被转换内存表现形式。



这方法可能,然而,导致与调试器的一些奇怪的错误。如果你的字节数组不是很大,你可以使用 Array.ConvertAll 代替。

 为sbyte [] =签署Array.ConvertAll(无符号,b =>取消选中((为sbyte)b)条); 


I tried to convert an Array from byte[] to sbyte[].

Here is my sample Array:

byte[] unsigned = { 0x00, 0xFF, 0x1F, 0x8F, 0x80 };

I already tried this:

sbyte[] signed = (sbyte[])((Array)unsigned);

But it doesn't work. There is no value in the array after this operation.

Does anybody has an better idea?

解决方案

sbyte[] signed = (sbyte[]) (Array) unsigned;

This works because byte and sbyte have the same length in memory and can be converted without the need to alter the memory representation.

This method might, however, lead to some weird bugs with the debugger. If your byte array is not very big, you can use Array.ConvertAll instead.

sbyte[] signed = Array.ConvertAll(unsigned, b => unchecked((sbyte)b));

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

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