I have to do the following in C
I need to
A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
E) Convert it to Ascii
F)And put in the original file.
Till now I just know how to open a file
FILE *fopen(const char *filename, const char *mode);
like i have coeded as f=fopen("/user/binaryfile","rb")
to read i have used
ret = fread(ptr,1,2,f);
where ptr is
byte ptr[2];
But i am not sure of how to use binary arrays how to get value from
these and also how to write data in these
which will then be used to write data in the file
I have to write a filed of length 13 eg 0604040404011
I also know the offset of the fleld
I am exploring fseek with SEEK_SET for the beginning of the file
main problem currently is handling of binary arrays (Byte array)
Here is my complete code
#include<stdio.h>
void main(){
typedef unsigned char byte;
/* Define the "byte" type name to be an unsigned char. */
byte ptr[2];
/* Declare a two byte array. */
int ret;
FILE* f; // create a new file pointer
if((f=fopen("/user/binaryfile","rb"))==NULL) { //
printf("could not open file"); // print
exit(1);
}
ret = fread(ptr,1,2,f);
printf("%d",ret);
printf("%o",*ptr);
fclose(f); // close the filepointer
}
Please tell me how to convert the binary data to Ascii/Hex data and
also back to binary
Thanks