How to convert an hex to a char

Good day all,

I actually have an issue :slight_smile:

I can get the CellId info of my loccation:

0,228,03,1771,2d6f,55,62

To get that value, I have to send AT+CENG=3 and then AT+CENG?
the I read the serial (_cell.read()) and store the response into buffer (char buffer[100]).

When finish, buffer dsplay

0,228,03,1771,2d6f,55,62

However, when I use those value to the OpenCellID web site (http://opencellid.org)(Go to Serach Location -> Cell Location) and I enter
mcc:228
mnc:3
lac:1771
cellid:2d6f,
it return me an error.

But if I convert 1771 and 2d6f from hexa to decimal and I try again it work!!

Then I have to find a way, to convert 1771 and 2d6f to have

0,228,03,6001,11631,55,62.

At a point to my code and before sending the data, I have this

strncat(fix,"&lac=",BUFFERSIZE);
    get_value(buffer,3,",",get_val); // This will take 3rd value of buffer (1771)
// How to convert get_val value from hex to dec and to put it into fix which is a char
    strncat(fix,get_val,BUFFERSIZE); // and concatenate it to fix

At this point, how can I convert 1771 to 11631?. get_val is a char and contian 1771. And I would like concatenate 11631 instead of 1771. (1771 is the hex of 11631.

Do you see my worry?

I actually have an issue

Good, because you certainly haven't got a clue, as evidenced by the thread title.

how can I convert 1771 to 11631?

You can't. You CAN interpret the data in the buffer to an long, and then output the long in whatever format you like.

   char valStg[10];
   long val = strtoul(get_val);
   sprintf(valStg, "%ld", val);

       strncat(fix,get_val,BUFFERSIZE); // and concatenate it to fix

Dear Pauls,
Thank for helping

I tried your exemple, but without success

Serial.println(get_val); // Should print 1771

    char valStg[10];
    long val = strtoul(get_val,NULL,0);  // strtoul(get_val); the terminal return me an error
    sprintf(valStg, "%ld", val);

     Serial.println(valStg);  // Should print 6001 but it display 1771

    strncat(buffer,valStg,BUFFERSIZE);
long val = strtoul(get_val,NULL,16);

Greate it's work!!
It work for 1771 and 2d6f. Well done!

But could you explain me what does/means the 16?

I suppose, as long as the value has 4 digit, I have to use 16?
If LAC is 9999, I should use 16?

Cheers

pierrot10:
Greate it's work!!

But could you explain me what does/means the 16?

I suppose, as long as the value has 4 digit, I have to use 16?
If LAC is 9999, I should use 16?

Cheers

16 is the number base for hexadecimal notation. Humans usually use 10, and computers use 2.

The hex digits are "0123456789ABCDEF". So 9999 is a valid hex number.

Ok, then I understand.
As I am sure it's hexa, I always will use 16 beacuse it's hexa. nothning to do with the hexa value
That right.

Thank a lot

When base is 0, C/C++ syntax is used: 0xNNNN is hexadecimal, 0NNN is octal, otherwise decimal.

oqibidipo:
When base is 0, C/C++ syntax is used: 0xNNNN is hexadecimal, 0NNN is octal, otherwise decimal.

When base is 0? When would that EVER be the case?

PaulS:
When base is 0? When would that EVER be the case?

He means "when the value 0 is passed as the parameter for base".

long val = strtoul(get_val,NULL,0);