I'm glad my suggestion was useful to you
However, although it's something minor I still will point out that I've made some little mistakes aligning header with data (to make it look more like a table), as seen here:
neiklot:
And here's some output:
.... sd card hex dump ....
Created: 10:29:14, Mar 27 2019
Initializing SD card... Card initialized.
File opened
File: blah001.dat
Offset Hexadecimal ASCII
00000000 31 38 39 2C 31 33 36 2C 31 35 32 2C 61 62 63 0D 189,136,152,abc.
00000010 0A 32 30 31 2C 31 35 32 2C 31 36 30 2C 78 79 7A .201,152,160,xyz
00000020 0D 0A 31 34 39 2C 31 31 35 2C 31 32 36 2C 65 74 ..149,115,126,et
00000030 63 0D 0A 31 35 34 2C 31 31 39 2C 31 32 34 0D 0A c..154,119,124..
00000040 31 33 33 2C 31 31 30 2C 31 31 35 0D 0A 31 31 38 133,110,115..118
00000050 2C 39 32 2C 39 36 0D 0A 31 32 37 2C 31 30 37 2C ,92,96..127,107,
00000060 31 31 30 0D 0A 39 30 2C 37 32 2C 37 39 0D 0A 0D 110..90,72,79...
00000070 0A .
File closed
The header is OK, it's just that I forgot to place an extra space between sections (between "Offset", "Hexadecimal" and "ASCII"); so the corrected function should look like this:
void dumpTheOpenedFile()
{
const byte bytesPerRow = 16; // 4 as minimum
char hexVal[9];
byte buffer[bytesPerRow];
Serial.print(F("File: "));
Serial.println(file.name());
Serial.println();
// Writting header
Serial.print(F("Offset "));
Serial.print(F("Hexadecimal ")); //added )
// Pad with spaces depending on the amount of bytes per row (it should align with the text below)
for (unsigned int spaces = (bytesPerRow - 4) * 3; spaces; spaces--)
{
Serial.write(' '); //put inside a {}
}
Serial.println(F(" ASCII"));
file.seek(0);
// Typing file's content
while (file.available())
{
// Print offset
sprintf(hexVal, "%08lX", file.position());
Serial.print(hexVal);
Serial.print(F(" ")); // to leave two spaces between offset and first hex character
byte amount = file.read(buffer, bytesPerRow);
// Print hex values
for (byte i = 0; i < amount; i++)
{
sprintf(hexVal, "%02X ", buffer[i]);
Serial.print(hexVal);
}
// Fill with spaces in case we couldn't fill an entire row (due to reaching the end of the file)
for (unsigned int spaces = (bytesPerRow - amount) * 3; spaces; spaces--)
Serial.write(' ');
Serial.write(' '); // Another extra space I've missed before
// Print ASCII values
for (byte i = 0; i < amount; i++)
{
// Printable characters appear as they are, non-printable appear as a dot/period (.)
Serial.write(buffer[i] > 31 && buffer[i] != 127 ? buffer[i] : '.');
}
Serial.println(); // Next line
} // End of while loop
file.close();
Serial.println(" **File closed**");
}
And... whoops, I've never placed before any space between the offset and the first hex value.
Well... being honest I've never actually tested what I wrote, so that's why I overlooked those mistakes.
Nevertheless, I'm still glad I've made something useful, so... you're welcome!