C语言 将字符指针转换为无符号字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13553752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert char pointer to unsigned char array
提问by user1007522
I want to convert a char pointer to a unsigned char var, I thought I could do that with just casting but it doesn't work:
我想将 char 指针转换为 unsigned char var,我以为我可以通过强制转换来做到这一点,但它不起作用:
char * pch2;
//Code that puts something in pc2
part1 = (unsigned char) pch2;
I've the code to this:
我有这样的代码:
result.part1 = (unsigned char *) pch2;
printf("STRUCT %s\n",result.part1);
result is just a struct with unsigned char arrays.
结果只是一个带有无符号字符数组的结构。
EDIT:
编辑:
pch2 = strtok( ip, "." );
while( pch2 != NULL ){
printf( "x %d x: %s\n", i, pch2 );
pch2[size-1] = 'typedef struct
{
unsigned char part1;
unsigned char part2;
unsigned char part3;
unsigned char part4;
} res;
';
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);
Struct:
结构:
part1 = (unsigned char*) pch2;
回答by iabdalkader
you cast to unsigned charnot unsigned char*you forgot the *
你投给unsigned char没有unsigned char*你忘记了*
pch2[size-1] = 'typedef struct
{
const char *part1;
const char *part2
const char *part3;
const char *part4;
} res;
'; /* note single quote */
result.part1 = (unsigned char *) pch2;
if pch2is not null terminated the program will crash, if you're lucky, when you use strlen, so you need to null terminate it first before printing using pch2, try this instead:
如果pch2不是空终止程序会崩溃,如果你很幸运,当你使用时strlen,所以你需要在打印使用之前先空终止它pch2,试试这个:
result.part1 = pch2;
Update: define your structure like so:
更新:像这样定义你的结构:
part1 = (unsigned char*) pch2;
And assign to it without casting at all:
并分配给它,根本不进行强制转换:
part1 = (unsigned char) pch2;
回答by piokuc
You want to do this:
你想这样做:
char *ph2;
unsigned char *new_pointer = (unsigned char*) ph2;
Instead of:
代替:
unsigned char part1 = (unsigned char)*pch2;
回答by Rahul Tripathi
Try something like this:-
尝试这样的事情:-
res result;
char* ip = "123.23.56.33";
sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &result.part1, &result.part2, &result.part3, &result.part4);
回答by Maciek
I want to convert a char pointer to a unsigned char var
我想将 char 指针转换为 unsigned char var
Are you sure? Converting pointer to char to unsigned char is not going to do any good - value will get truncated to 1 byte, and it will be meaningless anyway. Maybe you want to dereferencea pointer and get value pointed by it - then you should do something like this:
你确定吗?将指向 char 的指针转换为 unsigned char 不会有任何好处 - 值将被截断为 1 个字节,无论如何它都毫无意义。也许你想取消引用一个指针并获得它所指向的值 - 那么你应该做这样的事情:
unsigned int temp;
temp = atoi(pch2);
result.part1 = temp;
printf("Struct: %u\n",result.part1);
After your edit I see that part1is character array - if your program crashes after it is used, you probably fill pch2incorrectly. Maybe you forgot '\0'terminator?
在您编辑后,我看到这part1是字符数组 - 如果您的程序在使用后崩溃,您可能填写pch2不正确。也许你忘记了'\0'终结者?
EDIT:
编辑:
You see, it is much better now to answer your question having all required information. Do you need to use strtok? Would this be good?
你看,现在用所有必需的信息来回答你的问题要好得多。你需要使用strtok吗?这会好吗?
回答by user1007522
Found the problem, forgot to cast the char pch2 to unsigned int and then I can printout with %u. Code:
发现问题,忘了将 char pch2 转换为 unsigned int 然后我可以用 %u 打印输出。代码:
##代码##Thanks for your help guys!
谢谢你们的帮助!

