C#之SByte

int8

 

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);

 

posted on 2018-12-13 10:12  jiahuafu  阅读(7771)  评论(0编辑  收藏  举报

导航