Pages: [1]   Go Down
Author Topic: Reading ascii in hexadecimal, need it in decimal.  (Read 772 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10442
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And there's no hexadecimal anywhere...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it almost worked,

i first tried something like this :

Code:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
t does not compile with  strtoul()
We can't see the "it" that does not compile.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24364
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Still can't see it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it says invalid conversio from 'char' to 'const char'.
Then use a cast!
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit: sorry it was useless for what you want to do.. smiley
« Last Edit: October 16, 2012, 11:03:02 am by guix » Logged

Pages: [1]   Go Up
Jump to: