# Serial Data Conversion and Calculation

Morning Guys,

I am sure this is simple, as i am sure i am having some kind of mental block about this

I am sending characters (programmed in HEX) via Serial, and expecting a long string of characters as a response from a device.

I know i am getting the response, and if i Serial.write(DataLine[21]) into a terminal program that displays as HEX i get 8B. I need to convert 8B to a dec, which would be 139, then divide it by 10 to give a float 13.9. Thats the goal, and i intend to parse the whole string by position in a similar fashion (although math will vary a little)

The issue i am having is no matter what I use, "atoi", "int" i get zeros?? what am i missing. I am attaching the code so that you guys can review and tell me what on earth i am doing wrong...

Really appreciate any help - this is driving me crazy!

``````char DataLine [185];
char Convert[2];
static unsigned int input_pos = 0;
String inData;

void setup() {

Â  Serial2.begin(9600, SERIAL_8E1);
Â  Serial.begin(9600);

Â  Serial2.write(0xb8);
Â  Serial2.write(0x31);
Â  Serial2.write(0xf7);
Â  Serial2.write(0x02);
Â  Serial2.write(0x21);
Â  Serial2.write(0x01);
Â  Serial2.write(0x5C);

}

void loop() {

Â  if (Serial2.available()) {
Â  Â  const char recieved = Serial2.read();
Â  Â  DataLine [input_pos] = recieved;

Â  Â  Â  if (input_pos == 21)
Â  Â  Â  {
Â  Â  Â  Convert[1] = recieved;
Â  Â  Â  Convert[2] = '/0';
Â  Â  Â  Serial.println(DataLine[21],HEX);
Â  Â  Â  Serial.write(Convert);
Â  Â  Â  Serial.println();
Â  Â  Â  int number = atoi(Convert);
Â  Â  Â  Serial.println();
Â  Â  Â  Serial.print(number);

Â  Â  Â  }
Â   input_pos = input_pos + 1;
Â  }

Â
}
``````

Terminating nul character is '\0', not '/0'.

atoi() will not accept A..F; you can use strtol or strtoul.

Note:
read the full number 0x8B into the array, next convert.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

``````Serial.print(DataLine[21], HEX);
``````

If your device already sends 0x8B, you don't have to do anything except for the division.

``````byte x = 0x8B;
float fl = x / 10.0;
``````

PS
I might have misinterpreted your question earlier; you have now two solutions for slightly different problems

Actually, on re-reading the original supplied code, I would have written that as

``````float number = DataLine[21] / 10.0;