converting array bytes from serial.read to int variables

I am trying to adjust the demo code of the Catalex YX5300 mp3 player module for a kids mp3 player - a device with 11 buttons that plays mp3s for very small kids.

This is going very poorly, as the whole module is build on serial communication, and the example only works with serial monitor input.

I am trying to read the File Count for a specific folder, and I got as far as receiving a uint8_t array from the module, after sending the appropriate command string.

The array is defined like this:

ansbuf[0] is 0x7e
ansbuf[1] is 0xff
ansbuf[2] is 0x6
ansbuf[3] is 0x4e
ansbuf[4] is 0x0
ansbuf[5] is 0x0
ansbuf[6] is 0xa
ansbuf[7] is 0xfe
ansbuf[8] is 0xa3
ansbuf[9] is 0xef

The buffer entry ansbuf[6] contains the number of tracks - in this case, A (=10 tracks).

How can I convert this value to an decimal int?

0xa is 10. No conversion is needed.

If that does not solve the problem then you need to post the complete program.

...R

there should be a conversion, since the buffer array in question is defined as a static uint8_t

static uint8_t ansbuf[10] = {0}; // Buffer for the answers.

/* 
after sendCommand(), buffer is filled with 

0X7e 0Xff 0X06 0X4e 0X00 0X00 0X0a 0Xfe 0Xa3 0Xef

the byte "0X0A" contains the information (=10 files in the folder)
*/


 Serial.println("ansbuf[6] is 0x" + String(ansbuf[6], HEX));
// result: "ansbuf[6] is 0xa"
  

   
// problem: how to convert ansbuf[6] to int numberOfTracksInFolder ? 
   int numberOfTracksInFolder = String(ansbuf[6], HEX);
   Serial.println("int of ansbuf[6] is " + numberOfTracksInFolder);

 // error: cannot convert 'String' to 'int'

the complete code is attached, the problem appears in the “for”-loop right at the end

string_conversion_problem.ino (9.21 KB)

No conversion needed because ansbuf is an array of bytes

static uint8_t ansbuf[10] = {0}; // Buffer for the answers.

If you feel that you need to you can just copy ansbuf[6] to another variable but why bother ?

    int numberOfTracksInFolder = ansbuf[6]);

...I tried this at first actually, but got confused because Serial.print does not print the int value for some reason. But without my overtly verbose Serial.print-debugging, it seems to work

this does not display a number

 Serial.println("int of ansbuf[6] is " + numberOfTracksInFolder);
// console output: "int of ansbuf[6] is " (without numberOfTracksInFolder)

but this does

 Serial.println(numberOfTracksInFolder);

glad to see that ansbuf[6] is a number, now I can continue with the project. thank you for clearing this up!

nmarkus:
this does not display a number

 Serial.println("int of ansbuf[6] is " + numberOfTracksInFolder);

Because it’s nonsense. If you want to combine an integer and char array, use sprintf.

Beware though, the default installation of sprintf does not support the float type.

gfvalvo:
Because it's nonsense. If you want to combine an integer and char array, use sprintf.

Or just print it with 2 lines of code

Serial.print("int of ansbuf[6] is ");
Serial.println(numberOfTracksInFolder);

...R

Or don't even bother to put it in another variable

Serial.print("int of ansbuf[6] is ");
Serial.println(ansbuf[6]);