给char数组或者结构体赋值

首先,定一个一个结构体

struct ControlWindowMsg{

    unsigned char       buf1;

    unsigned char       buf2;

    

    unsigned char buf3;

    

    unsigned char       buf4;

    

};


定义一个char类型字符数组,并且赋值

 char buf[4] = {0};

    

    buf[0] = 1 & 0xff;

    buf[1] = 0 & 0xff;

    buf[2] = 3 & 0xff;

    buf[3] = 104 &0xff;


然后,将buf中的内容赋值给结构体

 struct ControlWindowMsg windowMsg;

    

    memset(&windowMsg, 0, sizeof(windowMsg)); //结构体赋值为0

    memcpy(&windowMsg, buf, sizeof(windowMsg)); //结构体赋值


通过打印可以发现,windowMsg赋值成功


ps:顺便添加一点

 NSTimeInterval timeInerval = [[NSDate date] timeIntervalSince1970];

    int taskId = [[NSString stringWithFormat:@"%f",timeInerval] intValue];

    

    buf[2] = taskId & 0xff;

    

    buf[3] = (taskId >> 8) & 0xff;

    

    buf[4] = (taskId >> 16) & 0xff;

    

    buf[5] = (taskId >> 24) & 0xff;


如果taksId是占四个字节的,给buf数组赋值的时候这样赋值


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值