C#byte 类型转 sbyte 类型

来源:互联网 发布:python多行注释 编辑:程序博客网 时间:2024/05/17 07:12

 C#中,byte为无符号8位整数,而Sbyte为有符号8位整数,对应java中的byte类型。 

方法一
byte 转为 sbyte。原理很简单,就是当 byte 小于 128 时其值保持不变,大于等于 128 时就将其减去 256。代码如下:

sbyte[] mySByte = new sbyte[myByte.Length];

for (int
 i = 0; i < myByte.Length; i++)
{
    
if
 (myByte[i] > 127)
        mySByte[i] = (sbyte)(myByte[i] - 256);
    
else

        mySByte[i] = (sbyte)myByte[i];
}

方法二

    byte[] tbyte = attribute.ByteValue;
     sbyte[] tsbyte = new byte[tbyte.Length];
     tsbyte = SupportClass.ToSByteArray(tbyte);