Converting String to a byte

Hello,
I’m new here and i just start working on arduino :slight_smile:
But i’ve an issue on my code.
I’ve got a string looking like : “154-test-test-456-0000”
and i need to convert it to a Hex (done) and i need to convert it again to a byte.
I need it because i’m writing this string into a NFC/RFID card on block but the function i use, expect a byte.
But i don’t know how to do that
Thanks

Blinkix

Just use a cast.

Thank for your reply
But i'm not very familiar with developpement, can you give me an exemple ?

A character represents a byte. Your string has 22 characters. So how do you want to calculate the byte? I'm going to be working with RFID tags this fall, and your application sounds interesting. Would you like to share your code? (You type "[code]" (no quotes!), then paste your code, then type "[/code]" to post code in this forum.)

The answer to my question may be in your code.

i just a MFRC522.h library and i just convert data into string, parse and decrement and i parse it into an byte to “upload it” to the card
and the probleme is on the last part

parse and decrement

I have no idea what that means.

Maybe you could show us, with your code.

Do you have a string (lower case s, a nul terminated character array, also known as c-string) or a String (capital S)? If the latter, rewrite your code to use the former; just to prevent vague problems when your code is running.

Once you use c-strings, there is basically no difference between a byte array and a char array. You can cast a character array to a byte array and vice versa.

Here is what i do

char value[16];
          NouvelleChaine.toCharArray(value,16);
         
          String varFinal;
          
          for (int i=0; i < NouvelleChaine.length()-1; i++){
              varFinal = varFinal+String(value[i],HEX);
               Serial.println(NouvelleChaine.substring(i,i+1));
                Serial.println(NouvelleChaine.substring(i,i+1));              
           }


           byte encodeChar;
           varFinal.getBytes(encodeChar,15);

NouvelleChaine is a String is value is like i say on my first message

for (int i=0; i < NouvelleChaine.length()-1; i++){
              varFinal = varFinal+String(value[i],HEX);
               Serial.println(NouvelleChaine.substring(i,i+1));
                Serial.println(NouvelleChaine.substring(i,i+1));              
           }

You seem to be printing the same thing twice.

Why use Strings to do something the print method already knows how to do?

encodeChar is one byte; and I assume getBytes() expects an array of byte

Thanks awol, it's just for debug, i just wanted to know what it was

But how can i do for my hex string to a byte ?

So
i’ve made some improvement and the function i use wait this

MFRC522::StatusCode MFRC522::MIFARE_Write(	byte blockAddr, ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The page (2-15) to write to.
											byte *buffer,	///< The 16 bytes to write to the PICC
											byte bufferSize	///< Buffer size, must be at least 16 bytes. Exactly 16 bytes are written.
										) {

MIFARE_Write( blockAddr, (byte *)buffer, (byte) 16); Like I said, use a cast.

thank, i've done that

byte valueBlockEncodage    = 4;
          String varFinal;
          varFinal = "322d566963746f722d3439382d303030";
          
          status = mfrc522.MIFARE_Write(valueBlockEncodage, (byte *)&varFinal, (byte)16);

but it do not write correctly my varfinal it's look weird like


in hexa code :
FF : 04: 20:00:20:00:06:04:03:00:03:00:bb:04:10:00

varfinal is a String, not a char array.

oh so i have to translate it ? how ?
sorry if it sound stupid but i'm a begginer
Update
i've test with

byte dataBlock[]    = {
              0x32, 0x2d, 0x56, 0x69, 
              0x63, 0x74, 0x6f, 0x72, 
              0x2d, 0x34, 0x39, 0x38,
              0x2d, 0x30, 0x30, 0x30 
          };

and it work
BUT i need to create this var dynamically with a string

Use toCharArray.

Better still, stop using String.

i 've done that

byte byteArr[16];
           varFinal.toCharArray(byteArr,16);
status = mfrc522.MIFARE_Write(valueBlockEncodage, byteArr, (byte)16);

but it writing “33 32 32 64 35 36 36 39 36 33 37 34 36 66 37” in hexa
and “322d566963746f7” in utf8
but i wanted “322d566963746f722d3439382d3030” in hexa