[英]Convert char pointer to unsigned char array
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指针转换为一个无符号的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. 结果只是一个带有无符号char数组的结构。
EDIT: 编辑:
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);
Struct: 结构:
typedef struct
{
unsigned char part1;
unsigned char part2;
unsigned char part3;
unsigned char part4;
} res;
you cast to unsigned char
not unsigned char*
you forgot the *
您转换为unsigned char
不是unsigned char*
您忘记了*
part1 = (unsigned char*) pch2;
if pch2
is 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
不为null终止,则该程序将崩溃,如果很幸运,当您使用strlen
,因此在使用pch2
打印之前,您需要先对其进行null终止,请尝试以下操作:
pch2[size-1] = '\0'; /* note single quote */
result.part1 = (unsigned char *) pch2;
Update: define your structure like so: 更新:像这样定义您的结构:
typedef struct
{
const char *part1;
const char *part2
const char *part3;
const char *part4;
} res;
And assign to it without casting at all: 并完全不进行任何分配就分配给它:
result.part1 = pch2;
You want to do this: 您想这样做:
part1 = (unsigned char*) pch2;
Instead of: 代替:
part1 = (unsigned char) pch2;
Try something like this:- 试试这样的东西:
char *ph2;
unsigned char *new_pointer = (unsigned char*) ph2;
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. 将指针从char转换为unsigned char不会有任何好处-值将被截断为1个字节,而且无论如何将毫无意义。 Maybe you want to dereference a pointer and get value pointed by it - then you should do something like this: 也许您想取消引用指针并获得其指向的值-那么您应该执行以下操作:
unsigned char part1 = (unsigned char)*pch2;
After your edit I see that part1
is character array - if your program crashes after it is used, you probably fill pch2
incorrectly. 编辑后,我看到part1
是字符数组-如果您的程序在使用后崩溃,则可能是错误地填充了pch2
。 Maybe you forgot '\\0'
terminator? 也许您忘记了'\\0'
终止符?
EDIT: 编辑:
You see, it is much better now to answer your question having all required information. 您会发现,现在回答包含所有必需信息的问题会更好。 Do you need to use strtok
? 您需要使用strtok
吗? Would this be good? 这样好吗?
res result;
char* ip = "123.23.56.33";
sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &result.part1, &result.part2, &result.part3, &result.part4);
Found the problem, forgot to cast the char pch2 to unsigned int and then I can printout with %u. 发现问题,忘记将char pch2强制转换为unsigned int,然后可以使用%u打印输出。 Code: 码:
unsigned int temp;
temp = atoi(pch2);
result.part1 = temp;
printf("Struct: %u\n",result.part1);
Thanks for your help guys! 感谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.