Print padded HEX with SDFat

CovertOne

New member
Hello all,

I am trying to use the SDFat library to log data on an SD card on a teensy 4.1 at a fast rate. To do this, I am looking to write data in HEX format. In the examples, specifically the teensySDIOLogger, the ringbuf.h library included with SDFat is used to store data until enough has accumulated to write a block to the SD card. The example uses "print" and "write" functions to populate the buffer. I cannot seem to find how to make these into padded hexadecimals as you can using the usual "print" functions for things like Serial. Is there something I am missing? I am pretty new to this stuff, so I still miss a lot. Included is the specific example I am referencing.
 

Attachments

  • TeensySdioLogger.ino
    4 KB · Views: 14
If you wish to convert a number to hex, there are many different ways to do so.

You could use something like sprintf to do so.

like:
Code:
char szTemp[10];
sprintf(szTemp, "%04X", my_val);

You can simply roll your own...
This was from Stackoverflow:
Code:
#define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i)

int x = SOME_INTEGER;
char res[5];

if (x <= 0xFFFF)
{
    res[0] = TO_HEX(((x & 0xF000) >> 12));   
    res[1] = TO_HEX(((x & 0x0F00) >> 8));
    res[2] = TO_HEX(((x & 0x00F0) >> 4));
    res[3] = TO_HEX((x & 0x000F));
    res[4] = '\0';
}
 
Back
Top