user1007522
user1007522

Reputation: 8118

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 * 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] = '\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;

Upvotes: 7

Views: 27117

Answers (5)

user1007522
user1007522

Reputation: 8118

Found the problem, forgot to cast the char pch2 to unsigned int and then I can printout with %u. Code:

unsigned int temp;

temp = atoi(pch2);

result.part1 = temp;
printf("Struct: %u\n",result.part1);

Thanks for your help guys!

Upvotes: 0

iabdalkader
iabdalkader

Reputation: 17332

you cast to unsigned char not unsigned char* you forgot the *

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[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;

Upvotes: 3

Maciek
Maciek

Reputation: 1716

I want to convert a char pointer to a 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 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. Maybe you forgot '\0' terminator?

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?

    res result;
    char* ip = "123.23.56.33";

    sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &result.part1, &result.part2, &result.part3, &result.part4);

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172608

Try something like this:-

 char *ph2;
 unsigned char *new_pointer = (unsigned char*) ph2;

Upvotes: 1

piokuc
piokuc

Reputation: 26204

You want to do this:

part1 = (unsigned char*) pch2;

Instead of:

part1 = (unsigned char) pch2;

Upvotes: 1

Related Questions