Decode 8 bytes hex

i only want to decode 8 bytes to hex, if it´s more or less than 8 bytes , I discard them.
the problem with this code is that if i write high it doenst show in hex high so that´s good but if i write high again it will be high+high=highhigh so that means 8 bytes but what i want is that if it is different from 8 bytes let´s say high(the word high) I want to discard it.

// C++ code
void setup()

void loop()
 if (Serial.available()== 8){
    int cantidad_bytes = Serial.available();
    byte datos[cantidad_bytes] = {0};
    Serial.readBytes(datos, cantidad_bytes); 
  for (int i = 0; i< cantidad_bytes; i++)  
    Serial.print(datos[i], HEX);
   //flush the buffer(serial.flush doesn´t work)

Sorry, I have no idea what this means.

If the serial input is coming from the serial monitor (PC), the input will consist of ASCII characters and possibly line endings, CR and LF, depending on what options you have set.

See this tutorial: Serial Input Basics - updated

I have to transform to hexadecimal if and only if I write a word that contains 8 bytes for example hellowor, if the word is different from 8 bytes I have to discard it.

How long do you want to wait from Serial.available( ) == 1 to Serial.available( )==8 if only 4 bytes arrive, and Serial.available( ) is stuck at 4?

You are not transforming anything. You are merely representing the binary value of the ASCII characters in human readable hexadecimal form.

If this is a school assignment, please post the actual assignment. It is fine to do so in your native language.

Yes it an school assigment but this is what i need, i have to representate the binary value in Hexadecimal only if the word contains 8 bytes, if it doesn't contain 8 bytes discard it(the word)

What binary value?

Either you do not understand the assignment, or you can't seem to explain it in English.

You said that in #5?

calculate a string of 8 characters entered by serial console and return the value in hexadecimal also by console

No, that does not work. You have to read whatever is in the serial buffer and save the character until you have 8 characters as you are doing, and then continue to read until you have read the line ending character/characters. CR/LF. Then you can do you Serial.println().

Something like #4?

How are you going to test this ?
Are you going to type a number of characters into the serial console then press enter, then test if 8 characters are there, then if exactly 8 characters are present, convert to Hexadecimal or what ?

you seem confused about how to read input.

  • define a char buffer long enough to hold you input (20 > 8)
  • define an index into the buffer initialized to zero
  • check if input is available
    if (Serial.available())
  • when available, read the input char into the buffer at the index location
    buf [index] =;
  • increment the index
  • check if 8 characters have been read and process the buffer ("return value in hexadecimal") and reset the index to zero

do you mean I can type in A123B456 (8 chars) and you need to compute the uint32_t integer 0xA123B456?
what's the line ending? What marks the end of an input?

Indeed I would suggest to study Serial Input Basics to understand how to deal with Serial input with an end marker

What does it mean to "calculate a string"?

The value of what? Of each char? Of the sum of them? What's the value of "Hello" for you? What's the value of "world"?

Can you fill up the following table with the values you are expecting?

I have:                      I want:
"hello"                      ......
"world"                      ......
"hello world\n"              ......
"$%&?!#!"                    ......
"01234567"                   ......

Or come up with your own examples.

I have: I want:
"hello" discard it(cos it isn't 8 bytes)
"world" discard it(same)
"hello world\n" discard it(same)
"$%&?!#!" discard it(same)
"01234567" 01234567(cos in hexadecimal is the same)
''Hellowor'' 68656c6c6f776f72

so you want to print the ASCII code of what's being typed, if exactly 8 chars have been submitted? How do you know the end of a submission?

I guess you have to read from the first char entered to last char entered(number 8),
Also if you enter a word that isn't 8 bytes long you have to discard it

that's the crux. How do you know it's finished to enter? hence the question we asked now MANY times.... is there an end marker of some sort...

please answer that question

No there isn't