iOS移动端开发ios实用技术iOS学习

进制转换

2016-08-20  本文已影响36人  Z了个L

参考链接

// Int 转 NSData
- (NSData *) setId:(int)Id {
//用4个字节接收
Byte bytes[4];
bytes[0] = (Byte)(Id>>24);
bytes[1] = (Byte)(Id>>16);
bytes[2] = (Byte)(Id>>8);
bytes[3] = (Byte)(Id);
NSData *data = [NSData dataWithBytes:bytes length:4];
}


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *data = [self setId:17052];
    NSLog(@"data = %@", data);
}

- (NSData *) setId:(int)Id {
    //用4个字节接收
    Byte bytes[4];
    bytes[0] = (Byte)(Id>>24);
    bytes[1] = (Byte)(Id>>16);
    bytes[2] = (Byte)(Id>>8);
    bytes[3] = (Byte)(Id);
    NSData *data = [NSData dataWithBytes:bytes length:4];
    return data;
}

@end


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *data = [self setId:17052];
    NSLog(@"data = %@", data);
}

- (NSData *) setId:(int)Id {
    //用4个字节接收
    Byte bytes[4];
    bytes[3] = (Byte)(Id>>24);
    bytes[2] = (Byte)(Id>>16);
    bytes[1] = (Byte)(Id>>8);
    bytes[0] = (Byte)(Id);
    NSData *data = [NSData dataWithBytes:bytes length:4];
    return data;
}

@end

上一篇 下一篇

猜你喜欢

热点阅读