Go Down

### Topic: Reading ascii in hexadecimal, need it in decimal. (Read 2396 times)previous topic - next topic

#### finthugo

##### Oct 16, 2012, 02:03 pm
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

#### PaulS

#1
##### Oct 16, 2012, 02:20 pm
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.
The art of getting good answers lies in asking good questions.

#### MarkT

#2
##### Oct 16, 2012, 02:21 pm
And there's no hexadecimal anywhere...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### finthugo

#3
##### Oct 16, 2012, 02:44 pm

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

#### finthugo

#4
##### Oct 16, 2012, 03:03 pm
it almost worked,

i first tried something like this :

Code: [Select]
`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

#### PaulS

#5
##### Oct 16, 2012, 03:19 pm
Code: [Select]
`      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.
The art of getting good answers lies in asking good questions.

#### finthugo

#6
##### Oct 16, 2012, 03:50 pm
it does not compile with  strtoul()
it says invalid conversio from 'char' to 'const char'.

#### AWOL

#7
##### Oct 16, 2012, 03:56 pm
Quote
t does not compile with  strtoul()

We can't see the "it" that does not compile.
"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.
I speak for myself, not Arduino.

#### finthugo

#8
##### Oct 16, 2012, 03:57 pm
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.

#### AWOL

#9
##### Oct 16, 2012, 04:00 pm
Still can't see it.
"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.
I speak for myself, not Arduino.

#### PaulS

#10
##### Oct 16, 2012, 04:23 pm
Quote
it says invalid conversio from 'char' to 'const char'.

Then use a cast!
The art of getting good answers lies in asking good questions.

#### guix

#11
##### Oct 16, 2012, 05:54 pmLast Edit: Oct 16, 2012, 06:03 pm by guix Reason: 1
Edit: sorry it was useless for what you want to do..

Go Up