Hex to Ascii converter

Hi All,
I have written code, and it works. The Problem is that the data Received is in Hex, and I require it in ASCII.

My Code:

#define RXD2 16
#define TXD2 17

float cell_2;

byte ByteArray[250];
int ByteData[20];

void setup() {

  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  //byte msg[]  = {0x7E, 0x01, 0x01, 0x00, 0x03, 0x0D}; 
  byte msg[]  = {0x7E, 0x35, 0x32, 0x30, 0x31, 0x34, 0x36, 0x34, 0x32, 0x45, 0x30, 0x30, 0x32, 0x30, 0x31, 0x46, 0x44, 0x33, 0x30, 0x0D}; 
  int i;
  int len = 20;

  Serial.println("SEND DATA ONE");
  for (i = 0 ; i < len ; i++) {
    Serial2.write(msg[i]);
    Serial.print("[");
    Serial.print(i);
    Serial.print("]");
    Serial.print("=");
    Serial.print(String(msg[i]));
  }
  len = 0;
  Serial.println();

  int a = 0;
  while (Serial2.available())
  {
    ByteArray[a] = Serial2.read();
    a++;
  }

  int b = 0;
  String registerHex;
  Serial.println("DATA RECEPTION");
  for (b = 0 ; b < a ; b++) {
    Serial.print(" ");
    Serial.print(b);
    Serial.print(" ");
    
    registerHex = String(ByteArray[b], HEX);
    Serial.print("Data: ");
    Serial.print(registerHex);
    Serial.print("");
    Serial.println();

  }
   
  Serial.println();

  Serial.println("REGISTERS HEX");

  ByteData[7] =  (ByteArray[17]) ;
  Serial.println(ByteData[7]);
  
  cell_2         = ByteData[7];

  Serial.print("Cell 2: ");
  Serial.print((cell_2 / 1000), 3);
  Serial.println(" V");

  delay(5000);

}

What I receive is the following:
12:56:50.564 -> REGISTERS HEX
12:56:50.564 -> 48
12:56:50.564 -> Cell 2: 0.048 V
12:56:55.587 -> SEND DATA ONE
12:56:55.587 -> [0]=126[1]=53[2]=50[3]=48[4]=49[5]=52[6]=54[7]=52[8]=50[9]=69[10]=48[11]=48[12]=50[13]=48[14]=49[15]=70[16]=68[17]=51[18]=48[19]=13
12:56:55.587 -> DATA RECEPTION
12:56:55.587 -> 0 Data: 7e
12:56:55.587 -> 1 Data: 35
12:56:55.587 -> 2 Data: 32
12:56:55.587 -> 3 Data: 30
12:56:55.587 -> 4 Data: 31
12:56:55.587 -> 5 Data: 34
12:56:55.587 -> 6 Data: 36
12:56:55.587 -> 7 Data: 30
12:56:55.587 -> 8 Data: 30
12:56:55.587 -> 9 Data: 34
12:56:55.587 -> 10 Data: 30
12:56:55.587 -> 11 Data: 38
12:56:55.587 -> 12 Data: 34
12:56:55.587 -> 13 Data: 30
12:56:55.587 -> 14 Data: 31
12:56:55.587 -> 15 Data: 31
12:56:55.587 -> 16 Data: 30
12:56:55.587 -> 17 Data: 30
12:56:55.587 -> 18 Data: 44
12:56:55.587 -> 19 Data: 37
12:56:55.587 -> 20 Data: 36
12:56:55.634 -> 21 Data: 30
12:56:55.634 -> 22 Data: 44
12:56:55.634 -> 23 Data: 31
12:56:55.634 -> 24 Data: 34
12:56:55.634 -> 25 Data: 30
12:56:55.634 -> 26 Data: 44
12:56:55.634 -> 27 Data: 35
12:56:55.634 -> 28 Data: 37
12:56:55.634 -> 29 Data: 30
12:56:55.634 -> 30 Data: 44
12:56:55.634 -> 31 Data: 35
12:56:55.634 -> 32 Data: 32
12:56:55.634 -> 33 Data: 30
12:56:55.634 -> 34 Data: 44
12:56:55.634 -> 35 Data: 36
12:56:55.634 -> 36 Data: 38
12:56:55.634 -> 37 Data: 30
12:56:55.634 -> 38 Data: 44
12:56:55.634 -> 39 Data: 35
12:56:55.634 -> 40 Data: 37
12:56:55.634 -> 41 Data: 30
12:56:55.634 -> 42 Data: 44
12:56:55.634 -> 43 Data: 35
12:56:55.634 -> 44 Data: 38
12:56:55.634 -> 45 Data: 30
12:56:55.634 -> 46 Data: 44
12:56:55.634 -> 47 Data: 35
12:56:55.634 -> 48 Data: 35
12:56:55.634 -> 49 Data: 30
12:56:55.634 -> 50 Data: 44
12:56:55.634 -> 51 Data: 35
12:56:55.634 -> 52 Data: 30
12:56:55.634 -> 53 Data: 30
12:56:55.634 -> 54 Data: 44
12:56:55.634 -> 55 Data: 35
12:56:55.634 -> 56 Data: 34
12:56:55.634 -> 57 Data: 30
12:56:55.634 -> 58 Data: 44
12:56:55.634 -> 59 Data: 36
12:56:55.669 -> 60 Data: 31
12:56:55.669 -> 61 Data: 30
12:56:55.669 -> 62 Data: 44
12:56:55.669 -> 63 Data: 35
12:56:55.669 -> 64 Data: 41
12:56:55.669 -> 65 Data: 30
12:56:55.669 -> 66 Data: 44
12:56:55.669 -> 67 Data: 34
12:56:55.669 -> 68 Data: 43
12:56:55.669 -> 69 Data: 30
12:56:55.669 -> 70 Data: 44
12:56:55.669 -> 71 Data: 35
12:56:55.669 -> 72 Data: 34
12:56:55.669 -> 73 Data: 30
12:56:55.669 -> 74 Data: 44
12:56:55.669 -> 75 Data: 36
12:56:55.669 -> 76 Data: 32
12:56:55.669 -> 77 Data: 30
12:56:55.669 -> 78 Data: 44
12:56:55.669 -> 79 Data: 35
12:56:55.669 -> 80 Data: 37
12:56:55.669 -> 81 Data: 30
12:56:55.669 -> 82 Data: 36
12:56:55.669 -> 83 Data: 30
12:56:55.669 -> 84 Data: 30
12:56:55.669 -> 85 Data: 34
12:56:55.669 -> 86 Data: 33
12:56:55.669 -> 87 Data: 30
12:56:55.669 -> 88 Data: 30
12:56:55.716 -> 89 Data: 34
12:56:55.716 -> 90 Data: 34
12:56:55.716 -> 91 Data: 30
12:56:55.716 -> 92 Data: 30
12:56:55.716 -> 93 Data: 34
12:56:55.716 -> 94 Data: 34
12:56:55.716 -> 95 Data: 30
12:56:55.716 -> 96 Data: 30
12:56:55.716 -> 97 Data: 34
12:56:55.716 -> 98 Data: 34
12:56:55.716 -> 99 Data: 30
12:56:55.716 -> 100 Data: 30
12:56:55.716 -> 101 Data: 34
12:56:55.716 -> 102 Data: 37
12:56:55.716 -> 103 Data: 30
12:56:55.716 -> 104 Data: 30
12:56:55.716 -> 105 Data: 34
12:56:55.716 -> 106 Data: 32
12:56:55.716 -> 107 Data: 30
12:56:55.716 -> 108 Data: 30
12:56:55.716 -> 109 Data: 30
12:56:55.716 -> 110 Data: 30
12:56:55.716 -> 111 Data: 31
12:56:55.716 -> 112 Data: 35
12:56:55.716 -> 113 Data: 35
12:56:55.716 -> 114 Data: 35
12:56:55.716 -> 115 Data: 32
12:56:55.716 -> 116 Data: 37
12:56:55.716 -> 117 Data: 42
12:56:55.716 -> 118 Data: 43
12:56:55.716 -> 119 Data: 30
12:56:55.716 -> 120 Data: 36
12:56:55.716 -> 121 Data: 32
12:56:55.716 -> 122 Data: 37
12:56:55.716 -> 123 Data: 42
12:56:55.716 -> 124 Data: 44
12:56:55.716 -> 125 Data: 30
12:56:55.762 -> 126 Data: 30
12:56:55.762 -> 127 Data: 31
12:56:55.762 -> 128 Data: 46
12:56:55.762 -> 129 Data: 36
12:56:55.762 -> 130 Data: 33
12:56:55.762 -> 131 Data: 36
12:56:55.762 -> 132 Data: 34
12:56:55.762 -> 133 Data: 46
12:56:55.762 -> 134 Data: 46
12:56:55.762 -> 135 Data: 46
12:56:55.762 -> 136 Data: 44
12:56:55.762 -> 137 Data: 30
12:56:55.762 -> 138 Data: 30
12:56:55.762 -> 139 Data: 30
12:56:55.762 -> 140 Data: 30
12:56:55.762 -> 141 Data: 46
12:56:55.762 -> 142 Data: 46
12:56:55.762 -> 143 Data: 46
12:56:55.762 -> 144 Data: 44
12:56:55.762 -> 145 Data: 45
12:56:55.762 -> 146 Data: 31
12:56:55.762 -> 147 Data: 36
12:56:55.762 -> 148 Data: 38
12:56:55.762 -> 149 Data: d
12:56:55.762 ->

I need to combine Line 17, 18, 19, 20 and get the ASCII Value

The print method knows how to convert appropriate types to ASCII

#define RXD2 16
#define TXD2 17

float cell_2;
byte ByteArray[250];
int ByteData[20];

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  //byte msg[]  = {0x7E, 0x01, 0x01, 0x00, 0x03, 0x0D}; 
 const byte msg[]  = {0x7E, 0x35, 0x32, 0x30, 0x31, 0x34, 0x36, 0x34, 0x32, 0x45, 0x30, 0x30, 0x32, 0x30, 0x31, 0x46, 0x44, 0x33, 0x30, 0x0D}; 
  int i;
  int len = 20;

  Serial.println("SEND DATA ONE");
  for (i = 0 ; i < len ; i++) {
    Serial2.write(msg[i]);
    Serial.print("[");
    Serial.print(i);
    Serial.print("]");
    Serial.print("=");
    Serial.print(String(msg[i]));
  }
  len = 0;
  Serial.println();

  int a = 0;
  while (Serial2.available())  {
    ByteArray[a] = Serial2.read();
    a++;
  }
  int b = 0;
  //String registerHex;
  Serial.println("DATA RECEPTION");
  for (b = 0 ; b < a ; b++) {
    Serial.print(" ");
    Serial.print(b);
    Serial.print(" ");
    Serial.print("Data: ");
    Serial.print(ByteArray[b], HEX);
    if(ByteArray[b]>32 && ByteArray[b]<128) {   Serial.print(" ");Serial.write(ByteArray[b]);}
    Serial.println();

  }
  Serial.println();
  Serial.println("REGISTERS HEX");
  ByteData[7] =  (ByteArray[17]) ;
  Serial.println(ByteData[7]);
  
  cell_2         = ByteData[7];
  Serial.print("Cell 2: ");
  Serial.print((cell_2 / 1000), 3);
  Serial.println(" V");
  delay(5000);
}

If I take Line 17, it is 30, convert to ASCII, it is 0.

12:56:55.587 -> 17 Data: 30
12:56:55.587 -> 18 Data: 44
12:56:55.587 -> 19 Data: 37
12:56:55.587 -> 20 Data: 36

ASCII on that is 0D76, converted back to DEC is 3446. That is what i am looking for. When I convert from hex to Dec, I get 01D088D8

Thank you. This is working. How would I add line 17, 18, 19, 20 to give me one reading?
Hex: 30 44 36 33
ASCII: 0 D 1 3
Translated to 3347?

where do you get "3347"?

The data that it is reading is as follows:
12:56:55.587 -> 17 Data: 30
12:56:55.587 -> 18 Data: 44
12:56:55.587 -> 19 Data: 37
12:56:55.587 -> 20 Data: 36
In hex. If I convert to the ASCII, it gives me 0D76, which if you convert back to Decimal is 3446. This is a very roundabout way, but If I convert from Hex to Dec, it does not give me the 3446

ah, ok. but where you need such strange conversing? what device do you try to communicate?

output

0D13
3347
D13


const char *s = "0D13";
int         val;

void setup (){
    Serial.begin (115200);

    sscanf (s, "%x", &val);

    Serial.println (s);
    Serial.println (val);
    Serial.println (val, HEX);
}

void loop (){
}

I am communicating to a 16Cell BMS. Getting the data from there. The Data Sending is as follows:
7E 35 32 30 31 34 36 34 34 45 30 30 32 30 31 46 44 32 45 0D

Data Receiving:
7E 35 32 30 31 34 36 30 30 34 30 38 34 30 31 31 30 30 44 30 39 30 44 30 39 30 44 30 39 30 44 3041 30 44 30 39 30 44 30 39 30 44 30 39 30 44 3039 30 44 30 39 30 44 30 39 30 44 30 39 30 44 3039 30 44 30 39 30 44 30 39 30 44 30 39 30 44 3039 30 36 30 30 34 30 30 30 34 30 30 30 34 30 3030 34 30 30 30 34 30 30 30 33 46 30 30 30 30 3134 44 42 30 30 30 30 30 36 32 37 31 30 30 30 3031 30 30 36 34 30 30 30 30 30 30 30 30 30 30 3030 45 32 38 42 0D

This is live capture from Serial Port Monitor with the BMS Software running in the back.

Thank you. The Data is read from a device and is variable. Its a battery Voltage that its reading from a Lithium BMS.

hopefully you realize that aside from the 0x7e at the starts and 0x0d (carrage return) at the end, the entire array is composed of ASCII characters representing hex digits.

 ~52014600408401100D090D090D090D0A0D090D090D090D090D090D090D090D090D090D090D090D090600400040004000400040003F000014DB000006271000010064000000000000E28B

need to know what the format is to decode. are every 4 digits a value?

very cool.
a char '0' can be translated to decimal 0 as x=x-48;
if it still larger as 9 the it is a big letter, then

if(x>47 && x<58)x-=48;
if(x>64 && x<71)x-=55;

so it will be from 0 to 15
then

y = x[0]*16+x[1];

grafik

FYI

Some of the data is every 2, and others are every 4. There is about 22 different values. most of them in 4 digits a value

so where is a description of that format?

one approach is to capture the data in to a structure making it easy to access the various sized data

I think I missed something here.. So the 15 would be for the 4 bytes of data reading?

I want to combine 17, 18, 19 and 20 so i am reading 0D09 and Serial.print("Value"); with value being the value of the received Voltage as 3446.

This is what Serial Monitor is giving me now:
14:28:35.859 -> 17 A: 0
14:28:35.859 -> 18 A: D
14:28:35.859 -> 19 A: 5
14:28:35.859 -> 20 A: C

I am working off a code I did for a similar project operating with Modbus and getting the same data. I have not got the document that gives all the data, but I know what the values are. Sounds strange I know, but I managed to get that one working.

The other program is purely Hex, and I added the data by the following data:

Serial.println(ByteData[1], DEC);
  ByteData[2] =  ByteArray[8] * 256 + ByteArray[9];
#define RXD2 16
#define TXD2 17

float cell_2;
byte ByteArray[250];
int ByteData[20];

void setup() {
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
  //byte msg[]  = {0x7E, 0x01, 0x01, 0x00, 0x03, 0x0D};
  const byte msg[]  = {0x7E, 0x35, 0x32, 0x30, 0x31, 0x34, 0x36, 0x34, 0x32, 0x45, 0x30, 0x30, 0x32, 0x30, 0x31, 0x46, 0x44, 0x33, 0x30, 0x0D};
  int i;
  int len = 20;

  Serial.println("SEND DATA ONE");
  for (i = 0 ; i < len ; i++) {
    Serial2.write(msg[i]);
    Serial.print("[");
    Serial.print(i);
    Serial.print("]");
    Serial.print("=");
    Serial.print(String(msg[i]));
  }
  len = 0;
  Serial.println();

  int a = 0;
  while (Serial2.available())  {
    ByteArray[a] = Serial2.read();
    a++;
  }
  int b = 0;
  Serial.println("DATA RECEPTION");
  for (b = 0 ; b < a ; b++) {
    Serial.print(" ");
    Serial.print(b);
    Serial.print(" ");
    Serial.print("Data: ");
    Serial.print(ByteArray[b], HEX);
    if (ByteArray[b] > 32 && ByteArray[b] < 128) {
      Serial.print(" ");
      Serial.write(ByteArray[b]);
    }
    Serial.println();
  }
  long yyy=0;
  for (int i = 17 ; i < 21 ; i++) {
    byte x = ByteArray[i];
    if (x > 47 && x < 58)x -= 48;
    if (x > 64 && x < 71)x -= 55;
    ByteArray[i] = x;
    yyy=yyy*16+x;
  }
  Serial.println(yyy);
  Serial.println("REGISTERS HEX");
  ByteData[7] =  (ByteArray[17]) ;
  Serial.println(ByteData[7]);

  cell_2         = ByteData[7];
  Serial.print("Cell 2: ");
  Serial.print((cell_2 / 1000), 3);
  Serial.println(" V");
  delay(5000);
}