我想将 char 指针转换为 unsigned char var,我想我可以通过强制转换来做到这一点,但它不起作用:
char * pch2;
//Code that puts something in pc2
part1 = (unsigned char) pch2;
我有这个的代码:
result.part1 = (unsigned char *) pch2;
printf("STRUCT %s\n",result.part1);
结果只是一个带有无符号字符数组的结构。
编辑:
pch2 = strtok( ip, "." );
while( pch2 != NULL ){
printf( "x %d x: %s\n", i, pch2 );
pch2[size-1] = '\0';
if(i == 1)
result.part1 = (unsigned char *) pch2;
if(i == 2)
result.part2 = (unsigned char *) pch2;
if(i == 3)
result.part3 = (unsigned char *) pch2;
if(i == 4)
result.part4 = (unsigned char *) pch2;
i++;
pch2 = strtok (NULL,".");
}
printf("STRUCT %c\n",result.part1);
结构:
typedef struct
{
unsigned char part1;
unsigned char part2;
unsigned char part3;
unsigned char part4;
} res;