Reading ascii in hexadecimal, need it in decimal.

Hi all,

I got a sensor that send me 16 bits in ascii values, the sensor send the values in ascii (between 500 and 64000). If the sensor is on middle range (32000) i get with the fuction Serial1.read() that same values in hexadecimal value of the ascii so i recive 51 50 48 48 48 (3 2 0 0 0).
I need that value (32000) to make math fuctions with that so i cant use a string to do that, so, anyone have any ideia what can i do?

the sensor is an inclinometer 0729-1752

thanks all

I need that value (32000) to make math fuctions with that so i cant use a string to do that, so, anyone have any ideia what can i do?

You want to convert an ASCII string to an int. Seems like exactly what the atoi() function was invented for.

And there's no hexadecimal anywhere...

PaulS:
You want to convert an ASCII string to an int. Seems like exactly what the atoi() function was invented for.

Thanks PaulS i will search for atoi() function :smiley:

it almost worked,

i first tried something like this :

char c;
void loop() {
      int i = 0;
      char buf[32];
      portOne.write('x');
      delay(200);
      do {
            c = portOne.read();
            if(c != -1)
                  buf[i] = c;
            i++;
            delay(10);
      } while ( c != -1);
      Serial.print("x = ");
      Serial.print((buf));

and that gave anwers like
ldadswdnajsndlka x = 32000

the number was correct and i like that, but the just needed to put off the "iuhuiasdansdn",
then i tried with the atoi fuction just changing the last line for Serial.print(atoi(buf));
it that gave me:
x = 32000
x = -32000
its working for -500 to -32000 then it goes to 32000 and get lowering to 500 for the other side of the inclinometer.
That wasnt bad but what i need is 500 to 64000 (and thats what sensor send i tested it on realterm).
Any idea what it could be ?
thanks

      do {
            c = portOne.read();
            if(c != -1)
                  buf[i] = c;
            i++;
            delay(10);
      } while ( c != -1);
      Serial.print("x = ");
      Serial.print((buf));

buf is not a string. You should not be passing it to functions, like atoi() or Serial.print() that expect strings.

A string is a NULL terminated array of chars. buf IS an array of chars, but it is NOT NULL terminated, therefore it is NOT a string.

You need to fix that.

You should also learn about Serial.available().

You should also learn about sending end-of-record markers, and reading properly delimited records. Then, you would need that stupid delay() in the loop.

The atoi() function converts a string (which you don't have) to an int (a signed int). The strtoul() function deals with unsigned longs. That might be more useful.

it does not compile with strtoul()
it says invalid conversio from 'char' to 'const char'.

t does not compile with strtoul()

We can't see the "it" that does not compile.

sorry,
i mean it meaning by the code.
i tried the strtoul() fuction with the "c", and with the "buf" for both it does not compile.

Still can't see it.

it says invalid conversio from 'char' to 'const char'.

Then use a cast!

Edit: sorry it was useless for what you want to do.. :slight_smile: