Serial To array convert to HEX

I have 3rd party device which is sending RS485 and each packet start with 126 or 7E. When I try Serial.read(), I am Receiving decimal numbers not HEX, what I am trying to do is to convert that to hex compare it and put it in Array.(So I can analyze it later)

Sample of data retrieved “126516191692127126516191791126126516191694126126516191791126126”

What I need is something like this in array “7E 22FFAF16022161010E051A4BFA650216000000003B0118016D00940094003A0000F3
7E” (this is not above massage)

Below is my Code

#include <SoftwareSerial.h>

#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         13


SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX



byte byteSend;
String data;
int inData[250];
byte old_byteSend ;
int start_end = 0x7E;
int index = -1;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("SerialRemote");  // Can be ignored

  pinMode(Pin13LED, OUTPUT);
  pinMode(SSerialTxControl, OUTPUT);

  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver

  // Start the software serial port, to another device
  RS485Serial.begin(115200);   // set the data rate
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

  //Copy input data to output

  if (RS485Serial.available() > 0)
  {



    byteSend = RS485Serial.read();



    if (byteSend == 0x7E && old_byteSend == 0x7E ) {

      if ( start_end == 0 ) {
      //Serial.print("Start");
       index++;
       inData[index] = old_byteSend;
       start_end = 1;
      } else if (start_end == 1) {
         //Serial.print("END");

       //   Serial.println(index);
       index++;
       inData[index] = old_byteSend;
       start_end = 0;
       index = -1;
    }

    } else {
     
      index++;
      inData[index] = byteSend;

   
   }

    old_byteSend = byteSend;


  }// End If RS485SerialAvailable
//    Serial.println();
   for (int i = 0; i < 100; i++)
    {
     Serial.print(inData[i]);
   }
   Serial.println();
}//--(end main loop )---

Also how would you remove the extra array indexs with zeros.

Try converting from ASCII to HEX

byteSend = RS485Serial.read();

byteSend = byteSend - 0x30; // or - 48 (= 0x30), see www.asciitable.com

    if (byteSend == 0x7E && old_byteSend == 0x7E ) {[code]

I don't follow what you're asking about the array tho.

Thank you for the answer so after converting I am getting this:

Orginal, Converted
126,78
5,213
16,224
191,143
6,214
92,44
127,79
126,78
5,213
16,224
191,143
7,215
91,43
126,78
126,78
5,213
16,224
191,143
6,214
92,44
126,78

  1. How do you compare 78 to 7E
  2. By Array I meant after you putting your message in each array index you will have a lot of zeros which will print

Thank you for the answer so after converting I am getting this:

But, not posting any code.

How do you compare 78 to 7E

if(78 == 7E)
{
   Serial.println("A miracle occurred");

Shahreza:
When I try Serial.read(), I am Receiving decimal numbers not HEX,

Rubbish.

You receive bytes and for your sample you seem to print their values in decimal.

Thanks what I meant when you found the first 7E how then you put it into array until you found another 7E

So our array is look like:

7E.........7E

I am getting
7E......7E7E00000000000000000
......7E7E0000000000000000000

Sample of data retrieved "126516191692127126516191791126126516191694126126516191791126126"

How did you get the data? Did you print it using Serial.print? If so, just store the received byte in a byte array and it is done.

Your above text representation is decimal, hex is just a different representation. The number stays the same.
If you receive the character A, the decimal representation is 65 and the hexadecimal representation is 41.

And please be clever and write a space after each byte received; that way you can identify the individual bytes.

Setup a variable like readCount, increment it every time you actually read a character, then during your print loop, use the readCount as the upper bound.

sterretje:
How did you get the data? Did you print it using Serial.print? If so, just store the received byte in a byte array and it is done.

Your above text representation is decimal, hex is just a different representation. The number stays the same.
If you receive the character A, the decimal representation is 65 and the hexadecimal representation is 41.

And please be clever and write a space after each byte received; that way you can identify the individual bytes.

I am getting as decimal, but what I want is HEX so later I can go find data in there

Yes that just printed from serial received, but the problem with decimal I can not figure out the position of actual data since it moves (decimal does not print zeros behind data example: 05=> 5 Then position is moved )

Shahreza:
I am getting as decimal, but what I want is HEX

You still don’t get it. You get 8 bit binary data.

Not hex, dec, oct or bin, these attributes are used only for displaying the data.
(Edit: or for specifying constant values in source codes)

Here is an example using ‘-’ and not 0x7F as delimiter.

bool waitForFirst = true;
byte delimiter = '-'; //Edit: or 0x2d or 45
const byte maxMessage = 20;
byte message[maxMessage];
byte mPos = 0;

void setup() {
  Serial.begin(250000);
}

void loop() {
  if (Serial.available()) {
    byte val = Serial.read();
    if (waitForFirst) {
      if (val == delimiter) {
        addToBuffer(val);
        waitForFirst = false;
      }
    } else if (val == delimiter) {
      addToBuffer(val);
      processMessage(message, mPos);
      mPos = 0;
      waitForFirst = true;
    } else {
      addToBuffer(val);
    }
  }
}

void addToBuffer(byte val) {
  if (mPos < maxMessage) {
    message[mPos++] = val;
  }
}

void processMessage(byte* values, byte len) {
  Serial.print(F("len "));
  Serial.println(len);
  Serial.print(F("as chars '"));
  for(byte i=0; i<len; i++) {
    Serial.write(values[i]);
  }
  Serial.println(F("'"));
  Serial.print(F("in dec "));
  for(byte i=0; i<len; i++) {
    Serial.write(' ');
    Serial.print(values[i]);
  }
  Serial.println();
  Serial.print(F("in hex "));
  for(byte i=0; i<len; i++) {
    Serial.write(' ');
    Serial.print(values[i], HEX);
  }
  Serial.println();
}
len 10
as chars '-abcd1234-'
in dec  45 97 98 99 100 49 50 51 52 45
in hex  2D 61 62 63 64 31 32 33 34 2D

Obviously I did not understand it, but thank you for the help now I understand

Thank you