How to Store huge Hex values in SD Card?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Store huge Hex values in SD Card?

Jump to solution
2,691 Views
Amit_Kumar1
Senior Contributor II

Hi

I wanted to store some huge blocks of data in SD card, All the values are stored in a 8 bit buffer. Following is the layman code of what I am doing.

         

uint8_t buffer[300]; // Contains 8 bit int values from sensor/ ADC

int main(void)

{

SD_Card_init(); // In SPI mode with FATFs including ".hex" file created

FAT1_write(&fp1, buffer, sizeof(buffer), &bw); // Writing buffer to SD card

}

I wanted the values written to the file to be in hex.

When I am opening and reading the file through windows. I am not getting the same values as what I am able to see through any hex editor. In notepad it is showing some ascii characters. Following is the program for decoding the values in windows.

#include <stdio.h>

#include <stdlib.h>

#include <stdint-gcc.h>


int main()

{

FILE *fp;

uint8_t buffer [300];

int i=0;


fp = fopen(“buffer.hex", "r");

if(fp == NULL)

{

printf("file not found\n");

}

fread(buffer, 300, 1, fp);

for(i=0;i<300;i++)

{

printf("\n%d = %X \n",i, buffer[i]);

}

return 0;

}


Is there any better way to store huge hex values in SD card? as the values being read is different. (initial few values are as expected after that the o/p value changes). I think the problem is something related to ASCII char. as for 8 bit we need 0-255 but if it is converted to ascii then we have 0-127 decimal values defined after that the values are not standard. I don't know exactly what the issue is but initial values being correctly printed is below 127 dec or 0x7F hex values after the 1st occurrence of any hex value in file higher than 0x7F, the values changes till end.


I tried removing some initial values of buffer so that position of 1st occurrence of higher hex value changes. In the o/p again the same thing happened. Kindly help me in resolving this issue.

ascii table.jpg

Kind Regards

Amit kumar

0 Kudos
1 Solution
1,202 Views
BlackNight
NXP Employee
NXP Employee

you need to read the file in binary mode, otherwise it reads as ASCII and does CR/LF conversion too.

So instead of "r", try using "rb".

Erich

View solution in original post

0 Kudos
2 Replies
1,203 Views
BlackNight
NXP Employee
NXP Employee

you need to read the file in binary mode, otherwise it reads as ASCII and does CR/LF conversion too.

So instead of "r", try using "rb".

Erich

0 Kudos
1,202 Views
Amit_Kumar1
Senior Contributor II

Hi Erich

Thanks a lot !!!!. I have been struggling with this since last 2 days. It worked finally.

Kind Regards

Amit Kumar

0 Kudos