我写了个数组拼接函数,为什么得到的为什么不是预期的值
我代码如下:unsigned char DianYa={0xB5,0xE7,0xD1,0xB9,0x3A};
unsigned char DianYaZhi={0x32,0x2E,0x35,0x38};
void setup() {
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned char temp;
PinJieZhiFuChuan(DianYa,DianYaZhi,temp);
digitalWrite(13,HIGH);
Serial.write(temp,sizeof(temp));
digitalWrite(13,LOW);
delay(1000);
}
//拼接数组
void PinJieZhiFuChuan(unsigned char c1[],unsigned char c2[],unsigned char csum[])
{
int a=sizeof(c1);
int b=sizeof(c2);
int c=a+b;
int i;
for(i=0;i<c;i++)
{
if(i<a)
{
csum=c1;
}
else
{
csum=c2;
}
}
return;
}
预期串口打印出来的结果应该是:B5 E7 D1 B9 3A 32 2E 35 38
但串口打印出来的16进制结果为什么是:B5 E7 32 2E 00 B8 00 00 00
问题到底出在哪儿呢? 问题在 sizeof 上。你可以用print 打印出来看看这个值是不是你想要的。
unsigned char temp;
int a=sizeof(c1);
int b=sizeof(c2); 将代码修改如下,解决问题:
unsigned char DianYa={0xB5,0xE7,0xD1,0xB9,0x3A};
unsigned char DianYaZhi={0x32,0x2E,0x35,0x38};
void setup() {
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned char temp;
PinJieZhiFuChuan(DianYa,sizeof(DianYa),DianYaZhi,sizeof(DianYaZhi),temp);
digitalWrite(13,HIGH);
Serial.write(temp,sizeof(temp));
digitalWrite(13,LOW);
delay(1000);
}
//拼接数组
void PinJieZhiFuChuan(unsigned char c1[],int a,unsigned char c2[],int b,unsigned char csum[])
{
a=sizeof(c1);
b=sizeof(c2);
int c=a+b;
int i;
for(i=0;i<c;i++)
{
if(i<a)
{
csum=c1;
}
else
{
csum=c2;
}
}
return;
}
页:
[1]