应用场景:开辟四个字节,放入一个数,高位补零。
比如,我们需要把一张图片的长度上传到服务器,但是服务器规定必须用
8个字节表示。因此做如下处理:
NSData *imageData = UIImagePNGRepresentation(image);
Byte
byte[] = {
0,
0,
0,
0};
byte[
0] = (Byte)((imageData.length &
0xff000000) >>
24);
byte[
1] = (Byte)((imageData.length &
0x00ff0000) >>
16);
byte[
2] = (Byte)((imageData.length &
0x0000ff00) >>
8);
byte[
3] = (Byte)((imageData.length &
0x000000ff));
NSData *data1 = [[NSData alloc] initWithBytes:
byte length:
4];