您的位置:首页 > 其它

int转byte

2015-09-21 22:16 330 查看
- (NSData *)little_intToByteWithData:(int)i andLength:(int)len{

Byte abyte[len];

if (len == 1) {
abyte[0] = (Byte) (0xff & i);
}
else if (len ==
2) {
abyte[0] = (Byte) (0xff & i);
abyte[1] = (Byte) ((0xff00 & i) >>
8);
}
else {
abyte[0] = (Byte) (0xff & i);
abyte[1] = (Byte) ((0xff00 & i) >>
8);
abyte[2] = (Byte) ((0xff0000 & i) >>
16);
abyte[3] = (Byte) ((0xff000000 & i) >>
24);
}

NSData *adata = [NSData
dataWithBytes:abyte
length:len];

return adata;
}

//测试int转byte--int占8位4个字节

int testInt = 25549;

NSData *da = [self
little_intToByteWithData:testInt andLength:32];

NSLog(@"da--->%@",da);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: