ios Byte 转Int

作者: moodi | 来源:发表于2017-02-06 10:06 被阅读0次

方法一

Byte byte[] = [0x02,0x1E];
int result = byte[0]<<8|byte[1];

方法二

/**
 Byte转Int
 
 @param bytes byte
 @return int
 */
-(int)byteWithInt:(nullable const void  *)bytes
{
    Byte *byte = (Byte *)bytes;
    int returnValue = 0;
    int byteCount = sizeof(byte)/2;
    for (int i=0; i<byteCount; i++) {
        if (i == 0) {
            returnValue = byte[byteCount -1];
        }else{
            returnValue += pow(256, i) * byte[byteCount-i-1];
        }
    }
    return returnValue;
}

使用方法

     Byte byte[] = [0x00,0x1E];
     int result = [self byteWithInt:&byte];

推荐一个linux命令行网站:https://rootopen.com

相关文章

网友评论

    本文标题:ios Byte 转Int

    本文链接:https://www.haomeiwen.com/subject/wepyittx.html