Serial.readString(); to Decimal number

actual received value
// 01 04 1A 20 48 00 00 00 00 00 01 00 00 00 04 01 2C 00 00 01 F4 00 00 00 00 00 00 00 FF 78 71

01 2C == > Decimal number

I want to implement it.

Please read the sticky notes at the top of the forum and learn how to properly post your code. It will help people help you.

As for your code, it would be much more readable if you used char constants vs. arbitrary numbers. (e.g. Num < '0' vs. Num < 38). Also,

int(hexString.charAt(i));

already returns an integuer value so no need for 'int' and map is more complicated than necessary since if you want to map 'a'...'z' to 'A'..'Z' is it just '-26'

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

—-

Don’t post snippets (Snippets R Us!)

—-

Is this the received String

01 04 1A 20 48 00 00 00 00 00 01 00 00 00 04 01 2C 00 00 01 F4 00 00 00 00 00 00 00 FF 78 71

Do you really expect this to extract the separate numbers in your input?

Umm.. For the small of brain here. What are you trying to do? I totally missed that bit. And what is this string you have supposed to be?

-jim lee

I changed the question

So... every two blocks are a high and low byte of a number in hex? Is that what I'm reading?
Which one is high byte and which is low byte?

-jim lee

01 HI , 02 LOW

If you get it with (RS485_message = Serial.readString(); )

// Other Program Read Data
01 04 1A 20 48 00 00 00 00 00 01 00 00 00 04 01 2C 00 00 01 F4 00 00 00 00 00 00 00 78 71 FF

Just hex "01 02 " to Dec Convert
If I calculate "271" [
The expected value that should come out is "300" ( 01 == > 1 * 256 , 2C ==> 44 )
But it doesn't come out that way.

@uninanotech, it's extremely (and that is an understatement, there are other words for it too) impolite to edit posts after they have received a reply. You can do it if you're asked to add code tags, but that's about it.

Now the replies don't make sense to other people that stumble across your topic.

This was (one of) your earlier versions of the opening posts and I think it was basically the question.

if(Serial.available())
{
     RS485_message = Serial.readString();

     for( int i=0 ; i < 30; i++ )
     {
          RS485_Data_Str[i] = RS485_message.substring(i,i+1);
     }​

     **int T_15 = hexToDec(RS485_Data_Str[15]); // prediction 256**
     **int T_16 = hexToDec(RS485_Data_Str[16]); // prediction 44**
     **int T_156 = (T_15 * 256 ) + T_16;**

     Serial.print(STX);
     Serial.print("( ");
     Serial.print(T_15);
     Serial.print(",");
     Serial.print(T_16);
     Serial.print(" ),");
     Serial.print(T_156);
     Serial.print(ETX);
     Serial.println();
     delay(10);​
}

// Internet search content
unsigned int hexToDec(String hexString) {
unsigned int decValue = 0;
int nextInt;
     for (int i = 0; i < hexString.length(); i++) 
     {
          nextInt = int(hexString.charAt(i));
          if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
          if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
          if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
          nextInt = constrain(nextInt, 0, 15);
          decValue = (decValue * 16) + nextInt;
}
return decValue;
}

I would suggest to study Serial Input Basics to handle this