I'm trying to read a simple struct from a binary file, and turn it into hex.
I'm running into problems trying to print things out into the window. The "chunk" data is one big chunk, so I'm expecting it to print a lot of binary out into the window for the first printf and then hex for the 2nd printf. However, it just prints one line of an int that definitely isnt the hex it should be (it should be a very long char)
I'm wondering what I'm doing wrong? Do I have to iterate with a while loop over every byte and turn it into a byte_array before hexing? Or have I got my types wrong?
Here's my code:
void myChunks(){
struct chunkStorage
{
char chunk; // ‘Chunk of Data’
};
unsigned long e;
FILE *p;
struct chunkStorage d;
p=fopen(“myfile.txt”,”rb");
fread(&d.chunk,sizeof(d.chunk),1,p);
printf(d.chunk);
e = hex_binary(d.chunk);
printf(e);
fclose(p);
}
int hex_binary(char * res){
char binary[16][5] = {"0000", "0001", "0010", "0011", "0100", "0101","0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110","1111"};
char digits [] = "0123456789abcdef";
const char input[] = ""; // input value
res[0] = '\0';
int p = 0;
int value =0;
while(input[p])
{
const char *v = strchr(digits, tolower(input[p]));
if(v[0]>96){
value=v[0]-87;
}
else{
value=v[0]-48;
}
if (v){
strcat(res, binary[value]);
}
p++;
}
return res;
//printf("Res:%s\n", res);
}
int hex_binary(char * res){
...return res;}
...???? o.O – LPsv
isn'tNULL
after readingv[0]
doesn't make much sense. Dereferencing null pointer invokes undefined behavior. – MikeCATmyChunks()
is you declarechunkStorage
with a single characterchunk
as a member. (if you ever bothered to callmyChunks()
, youfread
1-byte intod.chunk
. So how do you ever expect a long string from1-byte
? This notwithstanding the fact that you never showmyChunks()
actually being called in your code. – David C. Rankin