I want to read eg. between 11th and 23rd number from hex .bin file looking like this: https://i.stack.imgur.com/9KJ1S.jpg, print some parts as intiger or other parts as a name (string). (preferably without using any [ ]
, only operations on pointers)
My example .bin file contains: first 4 hex numbers (blue highlight) is the length of name, then 2 numbers is name in ASCII. Next 4 numbers (blue underline) is the length of surname (red underline), and the last one is index.
My attempt:
After loading entire .bin file to buffer exactly like presented here: http://www.cplusplus.com/reference/cstdio/fread/ , I miserably tried in many ways to assign parts of this buffer to variables (or a structure) and then, printf it using formatting, just to see what got assigned.
char *name_length = malloc(4);
char *pEnd;
for(*buffer=0; *buffer<4; *buffer++) {
sscanf(buffer, "%s", name_length);
long int i = strtol (buffer, &pEnd, 16);
printf("%x", i);
}
Above (wrong) code prints 0000 (I imagine it is completely rotten from it's roots, though I don't know why); in case there was an elegant way to load buffer parts already to structure, here's declaration:
struct student_t
{
char name[20];
char surname[40];
int index;
};
The "closest" result I Could get is another code, which prints "2000." from my .bin file: "02 00 00 46 2E
" which means "2
0 0 0 /length/ F.
/string/"
for(int i=0; i<4; i++)
printf("%d", buffer[i]); //it's supposed to print first 4 hex digits...
for(int j=5; j<7; j++)
printf("%s", &buffer[j]); //it's supposed to print from 5th to 7th...
Thanks a lot for all the help and guidance.