Currently i am working on STM32F479 Eval board. I have to collect data from sensor nodes and store it in a text file using FatFs. My sensor values are in Hexadecimal values. I am using STM32F4Cube HAL Library which includes FatFs. I am using f_write() function to pass these hex values to a text file. But it is storing as garbage values which is not as expected. When i use f_printf() it is working perfectly as expected (Hex values gets stored). But when reading again it shows some other values which is not the stored values. I have two questions.

  1. What is the perfect way to store Hex values in a text file and read the same from a text file using FatFs?

  2. Is there any other way like converting these hex values to char before passing it into a file and again reconverting after reading from a file?

My code:

uint8_t Buff[]; // Buffer to read data from Sensors 

uint32_t byteswritten;

f_write(&MyFile,Buff,sizeof(Buff),(void*)byteswritten); //But writing some other values

    f_printf(&MyFile,Buff[i]); //Writing properly But reading throws unexpected values
  • 1
    \$\begingroup\$ Are you reading the file, tokenizing it, and running it through strtol()? Or are you just pulling the ASCII value that you stuffed into the file? \$\endgroup\$
    – Daniel
    Jul 13, 2016 at 5:53
  • 1
    \$\begingroup\$ I don't know what you mean by 'hex'. Do you mean the file looks something like "0d67f2a8569bc780", or "0d 67 f2 a8 56 9b c7 80", or "0x0d 0x67 0xf2 0xa8 0x56 0x9b 0xc7 0x80", or "0dh 67h f2h a8h 56h 9bh c7h 80h"? Can you show us a little of that that "f_printf" output looks like? How is Buff[] getting formatted as hex? It appears to me like you are just writing the sensor data directly without any formatting. Is the data coming from the sensors formatted? \$\endgroup\$
    – Mark
    Jul 13, 2016 at 6:04
  • \$\begingroup\$ I am getting data from our own chip (SoC) using SPI. SPI is configured as Receive only Slave 8 bits. So my data looks like "AA FF 80 12 A5 55". Yes, i am just pulling the ASCII value which are stuffed. \$\endgroup\$ Jul 14, 2016 at 6:10

1 Answer 1


This line makes a pointer from a random value on the stack, asking for a (Hard-) fault:


Correct code would look like this:

UINT byteswritten;

Note that f_write() writes data in binary to the file, not in hexadecimal.

That f_printf() line should give you a compiler warning like "make pointer from an integer without a cast", because it expects a string (pointer to char) as second argument.

Corrected line for hexadecimal output:

f_printf(&MyFile," %X",Buff[i]);
  • \$\begingroup\$ Thank you Turbo. Say my data looks like "AA FF AF FA 54 45" which is in Buff array(uint8_t). I have to store it into a text file as same as it is (Hex) and read back it into another array. How can this be implemented using FatFs? \$\endgroup\$ Jul 14, 2016 at 6:15
  • \$\begingroup\$ Try looking at how basic file operations like fread() and fwrite() work on a PC. \$\endgroup\$
    – Turbo J
    Jul 14, 2016 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.