extracting the 4 byte of char from char string and convert the first 4 byte to i

extracting the 4 byte of char from char string and convert the first 4 byte to i  using -'c'

I have a char array as string as char *x = "1234 is a test";
here 1234 the first 4 byte is a hex data and i need to extract the data and convert to the interger. Thus first I need to extract the first 4 byte as

char dest[5];
    strncpy(dest, x,4);
    char dest[5]='\0';

now how can i convert this 4 byte char to int and convert to the integer value??
is it possible???

i have tried using strtol(dest, &ptr,16 );

however strtol finds the numerical values and seperates but i need the exact first 4 byte to convert to integer

what i suppose is the data is obtained from network thus without using strtol, I suppose to use hex to decimal conversion. Thus i need to first pack 4 bytes in int type and use this . How to coalesced this 4 byte of char to int??

asked Sep 9, 2015 by Christin1871
0 votes

2 Answers

0 votes
 char *x = "1234 is a test";

Depending on whether you can rely on the fifth character not being a hex digit, you might skip the copying, and aim strtol() at x directly. It will stop parsing at the first character that is not a valid digit in the base you selected.

If you cannot rely on that, your copying code needs some cleaning up: char dest[5] declares an array with five elements, indices 0 through 4, so dest[5] = '\0'; would be setting a non-existent sixth element to zero, resulting in undefined behaviour. The digits will reside in dest[0] through dest[3] after copying, so it is really dest[4] that you would want to zero.

And drop the char in char dest[4] = '\0';, otherwise you are redeclaring dest...

char dest[5];
strncpy( dest, x, 4 );
dest[4] = '\0';

Somewhat more elegant would be:

char dest[5] = { 0 }; // actually sets *all* elements to zero
memcpy( dest, x, 4 );

i have tried using strtol(dest, &ptr,16 );

That should have worked, really. You did not show the code; from your comments that strtol() "finds the numerical values and seperates", I assume you were confused a bit and looked at ptr for the result. That is just a pointer at where strtol() stopped parsing. The result is returned by strtol():

char * ptr;
int i = strtol( dest, &ptr, 16 );

If you are not interested in the end pointer, you can just pass NULL:

int i = strtol( dest, NULL, 16 );

If, indeed, the first 4 bytes are not hex digits at all, but the binary representation of an int (which your last paragraph edited in later seems to imply), the way to go about it would be a union.

    int ival;
    char cval[4];
} convert;

strncpy( convert.cval, x, 4 );

int i = convert.ival;

This is, of course, assuming that sizeof( int ) == 4. If not, you're in trouble, unions are tricky that way.

answered Sep 9, 2015 by VictorMarqui
0 votes

Assuming first byte is the most significant byte:

int convert_char(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    else if (c >= 'A' && c <= 'F') {
        return (10 + c - 'A');
    else  if (c >= 'a' && c <= 'f') {
        return (10 + c - 'a');
    return 0;

char *x = "1234 is a test";
int y = 0;
y = convert_char(x[0]) * 16 * 16 * 16 + convert_char(x[1]) * 16 * 16 + convert_char(x[2]) * 16 + convert_char(x[3]);
answered Sep 9, 2015 by MaximiliaBay