C++ 如何在特定位置使用开关或if-else步骤将十六进制值分配给数组?
我在做一个爱好项目,它要求我根据特定的十六进制函数调用为数组分配十六进制值。但是,数组不会返回,只是在执行时冻结程序。寻找一些细节 然后我需要获取返回的无符号字符数组,然后将其移动到第二个没有空值的数组。这将在我的主程序中使用 谢谢, 奥雷利乌斯 函数将十六进制值指定给数组C++ 如何在特定位置使用开关或if-else步骤将十六进制值分配给数组?,c++,c,arrays,sorting,hex,C++,C,Arrays,Sorting,Hex,我在做一个爱好项目,它要求我根据特定的十六进制函数调用为数组分配十六进制值。但是,数组不会返回,只是在执行时冻结程序。寻找一些细节 然后我需要获取返回的无符号字符数组,然后将其移动到第二个没有空值的数组。这将在我的主程序中使用 谢谢, 奥雷利乌斯 函数将十六进制值指定给数组 unsigned char* command(unsigned char PIN,int sw,int size) { unsigned char* x; x = (unsigned cha
unsigned char* command(unsigned char PIN,int sw,int size)
{
unsigned char* x;
x = (unsigned char*)malloc(size);
//select line assignment to array
if(sw == 1)
{
if(PIN==0x00)
{
x[0]='0';
x[1]='0';
x[2]='0';
}
else if(PIN==0X01)
{
x[0]='1';
x[1]='0';
x[2]='0';
}
else if(PIN==0x02)
{
x[0]='0';
x[1]='1';
x[2]='0';
}
else if(PIN==0x03)
{
x[0]='1';
x[1]='1';
x[2]='0';
}
else if(PIN==0x04)
{
x[0]='0';
x[1]='0';
x[2]='1';
}
else if(PIN==0x05)
{
x[0]='1';
x[1]='0';
x[2]='1';
}
else if(PIN==0x06)
{
x[0]='0';
x[1]='1';
x[2]='1';
}
else if(PIN==0x07)
{
x[0]='1';
x[1]='1';
x[2]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
else if(sw == 2)
{
if(PIN==0x00)
{
x[3]='0';
x[4]='0';
x[5]='0';
}
else if(PIN==0X01)
{
x[3]='1';
x[4]='0';
x[5]='0';
}
else if(PIN==0x02)
{
x[3]='0';
x[4]='1';
x[5]='0';
}
else if(PIN==0x03)
{
x[3]='1';
x[4]='1';
x[5]='0';
}
else if(PIN==0x04)
{
x[3]='0';
x[4]='0';
x[5]='1';
}
else if(PIN==0x05)
{
x[3]='1';
x[4]='0';
x[5]='1';
}
else if(PIN==0x06)
{
x[3]='0';
x[4]='1';
x[5]='1';
}
else if(PIN==0x07)
{
x[3]='1';
x[4]='1';
x[5]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
else if(sw == 3)
{
if(PIN==0x00)
{
x[6]='0';
x[7]='0';
x[8]='0';
}
else if(PIN==0X01)
{
x[6]='1';
x[7]='0';
x[8]='0';
}
else if(PIN==0x02)
{
x[6]='0';
x[7]='1';
x[8]='0';
}
else if(PIN==0x03)
{
x[6]='1';
x[7]='1';
x[8]='0';
}
else if(PIN==0x04)
{
x[6]='0';
x[7]='0';
x[8]='1';
}
else if(PIN==0x05)
{
x[6]='1';
x[7]='0';
x[8]='1';
}
else if(PIN==0x06)
{
x[6]='0';
x[7]='1';
x[8]='1';
}
else if(PIN==0x07)
{
x[6]='1';
x[7]='1';
x[8]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
else if(sw == 4)
{
if(PIN==0x00)
{
x[9]='0';
x[10]='0';
x[11]='0';
}
else if(PIN==0X01)
{
x[9]='1';
x[10]='0';
x[11]='0';
}
else if(PIN==0x02)
{
x[9]='0';
x[10]='1';
x[11]='0';
}
else if(PIN==0x03)
{
x[9]='1';
x[10]='1';
x[11]='0';
}
else if(PIN==0x04)
{
x[9]='0';
x[10]='0';
x[11]='1';
}
else if(PIN==0x05)
{
x[9]='1';
x[10]='0';
x[11]='1';
}
else if(PIN==0x06)
{
x[9]='0';
x[10]='1';
x[11]='1';
}
else if(PIN==0x07)
{
x[9]='1';
x[10]='1';
x[11]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
else if(sw == 5)
{
if(PIN==0x00)
{
x[12]='0';
x[13]='0';
x[14]='0';
}
else if(PIN==0X01)
{
x[12]='1';
x[13]='0';
x[14]='0';
}
else if(PIN==0x02)
{
x[12]='0';
x[13]='1';
x[14]='0';
}
else if(PIN==0x03)
{
x[12]='1';
x[13]='1';
x[14]='0';
}
else if(PIN==0x04)
{
x[12]='0';
x[13]='0';
x[14]='1';
}
else if(PIN==0x05)
{
x[12]='1';
x[13]='0';
x[14]='1';
}
else if(PIN==0x06)
{
x[12]='0';
x[13]='1';
x[14]='1';
}
else if(PIN==0x07)
{
x[12]='1';
x[13]='1';
x[14]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
else if(sw == 6)
{
if(PIN==0x00)
{
x[15]='0';
x[16]='0';
x[17]='0';
x[18]='0';
}
else if(PIN==0x01)
{
x[15]='1';
x[16]='0';
x[17]='0';
x[18]='0';
}
else if(PIN==0x02)
{
x[15]='0';
x[16]='1';
x[17]='0';
x[18]='0';
}
else if(PIN==0x03)
{
x[15]='1';
x[16]='1';
x[17]='0';
x[18]='0';
}
else if(PIN==0x04)
{
x[15]='0';
x[16]='0';
x[17]='1';
x[18]='0';
}
else if(PIN==0x05)
{
x[15]='1';
x[16]='0';
x[17]='1';
x[18]='0';
}
else if(PIN==0x06)
{
x[15]='0';
x[16]='1';
x[17]='1';
x[18]='0';
}
else if(PIN==0x07)
{
x[15]='1';
x[16]='1';
x[17]='1';
x[18]='0';
}
else if(PIN==0x08)
{
x[15]='0';
x[16]='0';
x[17]='0';
x[18]='1';
}
else if(PIN==0x09)
{
x[15]='1';
x[16]='0';
x[17]='0';
x[18]='1';
}
else if(PIN==0x0A)
{
x[15]='0';
x[16]='1';
x[17]='0';
x[18]='1';
}
else if(PIN==0x0B)
{
x[15]='1';
x[16]='1';
x[17]='0';
x[18]='1';
}
else if(PIN==0x0C)
{
x[15]='0';
x[16]='0';
x[17]='1';
x[18]='1';
}
else if(PIN==0x0D)
{
x[15]='1';
x[16]='0';
x[17]='1';
x[18]='1';
}
else if(PIN==0x0E)
{
x[15]='0';
x[16]='1';
x[17]='1';
x[18]='1';
}
else if(PIN==0x0F)
{
x[15]='1';
x[16]='1';
x[17]='1';
x[18]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
return x;
}
else if(sw == 7)
{
if(PIN==0x00)
{
x[19]='0';
x[20]='0';
x[21]='0';
}
else if(PIN==0X01)
{
x[19]='1';
x[20]='0';
x[21]='0';
}
else if(PIN==0x02)
{
x[19]='0';
x[20]='1';
x[21]='0';
}
else if(PIN==0x03)
{
x[19]='1';
x[20]='1';
x[21]='0';
}
else if(PIN==0x04)
{
x[19]='0';
x[20]='0';
x[21]='1';
}
else if(PIN==0x05)
{
x[19]='1';
x[20]='0';
x[21]='1';
}
else if(PIN==0x06)
{
x[19]='0';
x[20]='1';
x[21]='1';
}
else if(PIN==0x07)
{
x[19]='1';
x[20]='1';
x[21]='1';
}
else
{
printf("\n Invalid hexa digit %u",(unsigned)PIN);
}
//selection command
return x;
}
}
主要功能
int main()
{
int sw = 1,i,size=22;
char S3=0,S2=1,S1=0,S0=1;
unsigned char PIN;
PIN=set_lpx_var(S0,S1,S2,S3);
unsigned char *x;
unsigned char *d;
x=command(PIN,sw,size);//sw = 1
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 2;
S3=0,S2=1,S1=1,S0=1;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 3;
S3=0,S2=1,S1=0,S0=0;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 4;
S3=0,S2=0,S1=1,S0=1;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 5;
S3=0,S2=1,S1=1,S0=0;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 6;
S3=1,S2=1,S1=1,S0=0;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
sw = 7;
S3=0,S2=1,S1=1,S0=0;
PIN=set_lpx_var(S0,S1,S2,S3);
x=command(PIN,sw,size);
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i];
}
}
for (i = 0; i < size; i++)
{
{
printf("Value at %i is %c",i,d[i]);
}
}
}
intmain()
{
int sw=1,i,尺寸=22;
字符S3=0,S2=1,S1=0,S0=1;
无符号字符引脚;
PIN=设定值(S0、S1、S2、S3);
无符号字符*x;
无符号字符*d;
x=命令(引脚、开关、大小);//开关=1
对于(i=0;i
无符号字符*d;// 一种快速优化第一个功能的方法,同时减少出错的可能性:
//Function to get hex condition
unsigned char set_lpx_var(char s0, char s1, char s2, char s3)
{
unsigned char result = (unsigned char)(((s3 != 0) << 3) + ((s2 != 0) << 2)
+ ((s1 != 0) << 1) + (s0 != 0));
printf("\n Obtained hexa digit %u", result);
return result;
}
//获取十六进制条件的函数
无符号字符集\u lpx\u var(字符s0、字符s1、字符s2、字符s3)
{
无符号字符结果=(无符号字符)((s3!=0)在发布问题时,请使用适当的缩进设置代码格式。您的代码很难阅读。此外,最好将主函数和全局函数放在一个代码块中,以便愿意帮助您的人可以更轻松地复制和粘贴到项目中并编译它。您是否使用调试器?如果使用调试器,您以前做了多少e程序开始出现错误?我真的很抱歉,因为我把代码按8空格规则剪切到了大小。完全没有看到代码块图标。问题是在你得到PIN十六进制值后开始的。一旦命令函数启动,屏幕就冻结了。我想这是一个松散的指针,但我不能太确定。真的吗我很欣赏你对这件事有一双新的眼光。创建一个表,列出PIN
值与S0
、S1
、S2
和S3
值的对比。你会看到一个模式或制作一个卡诺图来缩小它。一下子就错过了!我一定是困了。代码现在返回一个完整的0和1数组,以及一些垃圾值,比如y和smiley。新的没有被使用是因为我在使用Keil,而if-else噩梦是因为我的指针技能很弱。谢谢你的帮助!
unsigned char *d; // <-- d is an uninitialized pointer
x=command(PIN,sw,size);//sw = 1, ****x is assigned a block of memory within command so that's good****
for (i = 0; i < size; i++)
{
if (x[i]!='\0')
{
d[i]=x[i]; // ****<-- where did you assign d to a block of memory?****
}
}
//Function to get hex condition
unsigned char set_lpx_var(char s0, char s1, char s2, char s3)
{
unsigned char result = (unsigned char)(((s3 != 0) << 3) + ((s2 != 0) << 2)
+ ((s1 != 0) << 1) + (s0 != 0));
printf("\n Obtained hexa digit %u", result);
return result;
}
unsigned char* command(unsigned char PIN,int sw,int size)
{
unsigned char* x;
size_t offset = sw - 1;
if (PIN & 0x08) // hard to know from your code what to do in case
// of error here.
{
printf("\n Invalid hexa digit %u", PIN);
return NULL;
}
if (size < 22 || (sw < 1 || 7 < sw)) // avoid out-of-bounds errors
{
printf("\n Invalid buffer size %d", size);
return NULL;
}
x = (unsigned char*)malloc(size); // why not new ?
// why not passing an array for result?
if (!x)
return NULL;
memset(x, 0, size); // dynamic memory needs to be initialized.
//select line assignment to array
size_t offset = sw - 1;
x[offset + 0] = (PIN & 0x01) ? '1' : '0';
x[offset + 1] = (PIN & 0x02) ? '1' : '0';
x[offset + 2] = (PIN & 0x04) ? '1' : '0';
return x;
}