7

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;
2
  • 2
    In your title you ask for an array, in your text for a variable and most of the answers are showing you how to get a pointer to unsigned char. Which is it? Commented Nov 25, 2012 at 18:02
  • I'm having a pointer that I want to convert to a unsigned char array? Commented Nov 25, 2012 at 18:04

5 Answers 5

3

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;
Sign up to request clarification or add additional context in comments.

13 Comments

when printing out like a string the program stops running?
@user1007522 C strings should be null terminated strings of char not unsigned characters, what are you trying to do exactly ?
@user1007522: you can print a single char using %c. For example: printf("STRUCT %c\n", *result.part1);
Thanks when using %c it works like a charm. Do I need a for loop then to print everything?
@user1007522 no, you need to NULL terminate the string pch2[size-1]=0
|
1

You want to do this:

part1 = (unsigned char*) pch2;

Instead of:

part1 = (unsigned char) pch2;

Comments

1

Try something like this:-

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

2 Comments

I've done that, but when doing a printf %s the program stops? Is it something with strtok because pch2 is init by strtok ?
You can try with printf("STRUCT %c\n",result.part1);
1

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

8 Comments

I think this is closer to what the OP is looking for than the answers to date, but I'm still not sure the OP has been clear yet.
I've edited the code, I terminated the char array, like you see the first print of pch2 works like a charm. The last one not :(
crucial point here is - what is pointed by pch2 and how it gets there. Without knowing this, we cannot tell whether it is constructed correctly. Your way of terminating string is incorrect - "\0" definitely is not what should be there, and anyway you are calling strlen on potentially incorrect string. We need to know what value pch2 points to.
No, I can use sscanf, but the code crashes when Copy your line. Also my struct is not a unsigned char * x but just unsigned char x or that doesn't matter with the adress operator.
Then it would be helpful to see what is result structure and its part1...part4 members.
|
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.