Hello
I've googled this but I'm not getting much joy.
Does anyone know of an easy way to read/write hex from/to a file in C?
Thank you.
Hello
I've googled this but I'm not getting much joy.
Does anyone know of an easy way to read/write hex from/to a file in C?
Thank you.
fscanf(f, "%x", &var); maybe
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thank you for your reply.
So if I have a text file called plaintext.txt with the following HEX:
0123456789ABCDEF
Should the following code work?
Thank youCode:FILE *fp; unsigned char message[8]; fp=fopen("c:\\plaintext.txt", "r"); fscanf(fp, "%x", &message); fclose(fp);
Last edited by MrSteve; 03-25-2008 at 04:30 PM.
no...Should the following code work?
read the scanf format description
read about %x format
read about maximum integer value
try to figure out your problems...
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Here is the code, like yours, but it compiles:
Here is the output:Code:#include <stdio.h> int main(void) { FILE *fp; unsigned int message; fp=fopen("c:\\plaintext.txt", "r"); fscanf(fp, "%x", &message); printf("%x ", message); fclose(fp); return 0; }
Go from there. Looks to me like it's only printing the last 8 digits.Code:89abcdef