unsigned char cmd[] = {'A', 'T', '+', "0x07", "0x08", 'D', 'F', "0xA6"};
要将此命令写入目标设备,我希望写入字节
41 54 2b 07 08 44 46 A6
相应。
但是,写入设备的实际字节是
41 54 2b b8 bb 44 46 be
并且
显然是错误的0x07 0x08 0xA6
我希望在上面的例子中如何写字节?
-----写函数-------
void WriteToDevice(){
unsigned char cmd[]={'A', 'T', '+', "0x07", "0x08", 'D', 'F', "0xA6"};
int n = write(fd,&cmd,8);
for(p=0;p<8;p++){
printf("bytes written: %02x \n", cmd[p]);
}
if(n<0){
printf("write error \n");
}
else{
printf("%d bytes written \n", n);
}
};
编译器警告
warning: (near initialization for ‘cmd[3]’) [enabled by default]
warning: initialization makes integer from pointer without a cast [enabled by default]
答案 0 :(得分:2)
而不是"0x07", "0x08"
等,使用'\x07', '\x08
'等等