/*

  https://forum.arduino.cc/t/serial-read-string-hex-find-byte-buffer-to-hex-to-dec-as-a-float02/1049521/9

  Hex:  0x31 0x32 0x33 0x33 0x36
  Dec:   49  50  51  51  54
  float: 12.336

*/
int but = 4;

char buf [80];
char hexme [80];
char decme [80];
char fStr [10];

String data;
String hexjoin;
String decjoin;
char* rep1;
String rep2 = "";

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27, 16, 2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup (void)
{
  Serial.begin (9600);
  Serial.println();

  lcd.init();  // initialize the lcd
  lcd.backlight();
  //lcd.clear(); // dont use

  lcdprint();

  pinMode(but, INPUT_PULLUP);
}

void lcdprint()
{
  lcd.setCursor(0, 0);
  lcd.print ("ASCII TO HEX CONVERT"); // Start up message_row one
  lcd.setCursor(0, 1);
  lcd.print ("HEX: "); // Start up message_row two
  lcd.setCursor(0, 2);
  lcd.print ("DEC: "); // Start up message_row two
  lcd.setCursor(0, 3);
  lcd.print ("FLO: "); // Start up message_row two
}

void loop (void)
{
  if (Serial.available ())
  {
    int n = Serial.readBytesUntil ('\n', buf, sizeof(buf) - 1);
    buf [n] = '\0';         // terminal w/ NULL
    process (buf);
  }

/*
  if (Serial.read() > 0 )
  {
    // Clear all the variable values
    hexjoin = "";
    decjoin = "";
  }
*/

  if (digitalRead(4) == LOW)
  {
    delay(300);
    {
      if (digitalRead(4) == LOW)
      {
        //Serial.print(hexme);
        Serial.print ("HEX: ");
        Serial.println(hexjoin);
        Serial.print ("DEC: ");
        Serial.println(decjoin);
        Serial.print ("FLO: ");
        Serial.println(fStr); // char format
        Serial.println();
        delay(300);
      }
    }
  }

}

void process (char *buf )
{
  lcdprint();

  unsigned len = strlen (buf);


  // Process as HEX
  Serial.print ("HEX:  ");
  for (unsigned n = 0; n < len; n++)
  {
    //sprintf (hexme, " 0x%02x", buf[n]);  // send 123, result : 0x31 0x32 0x33
    sprintf (hexme, "%02x", buf[n]);       // send 123, result : 313233
    Serial.print (hexme);
    hexjoin += hexme;
  }
  Serial.println ();
  //Serial.print(F(" LSB : "));
  //Serial.println (hexjoin);
  lcd.setCursor(5, 1);
  lcd.print ("               "); // Start up message_row two
  //delay(20);
  lcd.setCursor(5, 1);
  lcd.print (hexjoin); // Start up message_row two



  // Process as DEC
  Serial.print ("DEC: ");
  for (unsigned n = 0; n < len; n++)
  {
    sprintf (decme, "%3d", buf[n]);
    Serial.print (decme);
    decjoin += decme;
  }
  Serial.println ();
  lcd.setCursor(4, 2);
  lcd.print ("                "); // Start up message_row two
  //delay(20);
  lcd.setCursor(4, 2);
  lcd.print (decjoin); // Start up message_row two



  // Process as FLOAT
  int   x = atoi (buf);
  float f = x / 1000.0;
  dtostrf (f, 6, 3, fStr);
  Serial.print ("FLO: ");
  Serial.println (fStr);
  lcd.setCursor(4, 3);
  lcd.print ("                "); // Start up message_row two
  //delay(20);
  lcd.setCursor(4, 3);
  lcd.print (fStr); // Start up message_row two



  // Clear all the variable values
  hexjoin = "";
  decjoin = "";

}