get a word from arduino and convert it to hex code and save it in an array

hi guys.
i want to get a word from serial monitor. i did it in code.

then i want to convert it to hex code. (for example get word:"hadi" and convert it to "68 61 64 69" (hex code)). i did it in code.

then i want to save all of the number in array that i can use each of it. (for example array a [16]= {6,8,6,1,6,4,6,9,20,20,20,20,20,20,20,20} (20 is a space =" ") and i want for example use a[1]=8 in another place)

my code:

char incomingByte [16];
String myValue [16];


void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial.println("hi. pls write a word :");
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}


void loop() {

  if (Serial.available() != 0)
  {
  incomingByte = Serial.read();
  String   myValue  =  String(incomingByte , HEX); //Convert it into Hexadecimal

  }
}

pls help complete this code.

We don't want to waste our time or your time fixing code if later you realise what you described is not what you really need. So please tell us what your project is and why you need to store the data this way. What will happen to the data after? What kind of Arduino are you using (because using String is a bad idea on Arduino like Uno, Nano etc).

PaulRB:
We don't want to waste our time or your time fixing code if later you realise what you described is not what you really need. So please tell us what your project is and why you need to store the data this way. What will happen to the data after? What kind of Arduino are you using (because using String is a bad idea on Arduino like Uno, Nano etc).

my project is to write a word (that come from serial monitor ) to a tag using rfid RC522. my arduino is mega.
to write a message on a tag, we should first convert the messge to a hexcode. then we should write that hex in a block of tag In hex code.
pls see these videos.
first video : https://www.youtube.com/watch?v=IlEWzpO2aFI ,
second video: https://www.youtube.com/watch?v=uihjXyMuqMY

now i need to get a word from serial.monitor. then i should convert it to a hex code in the form of a array to access each number of it (for example word "hadi" then convert to hex code = 68 61 64 69 and then save it in a array like array message [15] ={6,8,6,1,6,4,6,9,20,20,20,20,20,20,20,20}). then i should write the array on the block.

the structure of a tag has 16 sector and 64 blocks that each block (in except some blocks ) can store 2 byte date in the form of hexcode. (pls see the videos i pointed ).

if i want for example write "hadi" word to a tag, i should convert it to hex code (= 68 61 64 69 )
then i should write that code on rfid tag( into block bytes (using the first video) ) .

byte sector = 1;
byte blockAddr = 4;
byte dataBlock[] = { // NEW DATA to Write

message [0], message [1], message [2] message [3], message [4] message [5], message [6] message [7], // 1, 2, 3, 4, in this line we save hadi word in hex code
0xFF, 0xFF, 0xFF, 0xFF, // 5, 6, 7, 8,
0xFF, 0xFF, 0xFF, 0xFF, // 9, 10, 255, 12,
0xFF, 0xFF, 0xFF, 0xFF // 13, 14, 15, 16

so i should first get a word from serial monitor.
then i should convert it to hex number and store it in an array.
then i should write it to tag using first video program.

In the first video the data written is represented Hex in the program but it might just as well have been in bytes. Hex is just a way of writing data that makes it easier for humans to read and interpret. 0xFF is the same as 0b11111111 which is the same as decimal 255.

Presumably you want to write a representation of hadi to the tag to be read and verified later. If so then why not simply write the ASCII value of the 4 letters to the tag instead ?

hadimargo:
so i should first get a word from serial monitor.
then i should convert it to hex number ...

you dont need to convert it to hex, the incoming data is stored as bytes and bits on the microcontoller. HEX is a human-readable format! :slight_smile:

hadimargo:
...form of a array to access each number of it (for example word "hadi" then convert to hex code = 68 61 64 69 and then save it in a array like array message [16] ={6,8,6,1,6,4,6,9,20,20,20,20,20,20,20,20})...

so something like this in code maybe:

(compiles, not tested!)

byte incomingByte[8]; //max length of received word is 8 characters
byte message[16];

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial.println("hi. pls write a word :");
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}


void loop() {
  uint8_t i = 0, j = 0, k=0;

  while (Serial.available() != 0 && i < 8) {

    incomingByte[i++] = Serial.read();

  }

  if (i > 0) {
    //update message array with incoming bytes array as per requested format
    for (j = 0; j < 16; ++j) {
      if (k < i) {
        message[j] = (incomingByte[k] & 0xF0) >> 4;
        message[j + 1] = incomingByte[k] & 0x0F;
        ++j;
        ++k;
      }
      else {
        message[j] = 0x20;
      }
    }

    //print message array to serial monitor
    for (j = 0; j < 16; ++j) {
      Serial.print(message[j], HEX);
      Serial.print(", ");
    }
    Serial.println("");
  }
}

hope that helps...

The ASCII characters "hadi" are "68 61 64 69" when shown as HEX - there is no need to convert anything. This should illustrate that

if (Serial.available() > 0) {
   byte receivedByte = Serial.read();
   Serial.println(receivedByte, HEX);
}

...R