T
TazCoder
I need to figure out an algorithm to convert any file, into it's hex
representation in order to print out that file in windows forms. I do
not want byteviewer in this and that is why it is becoming a problem.
The algorithm needs to load up an existing file, convert it to hex,
(then save that hex representation into a temp file) output that file
into a richtextbox, and then close. Now I have an algorithm that works
well, but it is not perfect. For instance it will add a series of F's
at the end of each line. Here it is:
//temporary file to write the hexadecimal interpretation to
FILE * tempFile;
tempFile = fopen("temphex","w");
int count=0;
char c;
try {
//To read the file
ifstream hexReader ((CString)hexName);
fprintf(tempFile, " ");
for(int i=0; i<16;i++) {
if(i == 7) {
fprintf(tempFile, "%02X ",i);
} else {
fprintf(tempFile, "%02X ",i);
}
}
fprintf(tempFile, "\n\n");
//While there is still data to read from the file, read it
//and then write to the temp file the hex interpretation
int line = 0;
fprintf(tempFile, "%08X ", line);
line++;
while(hexReader.good()) {
c = (char)hexReader.get();
if(count == 7) {
fprintf(tempFile, "%02X ",c);
count++;
} else if(count == 15) {
fprintf(tempFile, "%02X \n%08X ",c,line);
count=0;
line++;
} else {
fprintf(tempFile, "%02X ",c);
count++;
}
}
//Close the reader
hexReader.close();
}
Any help on fixing just the hex conversion part of the algorithm would
be appreciated.
representation in order to print out that file in windows forms. I do
not want byteviewer in this and that is why it is becoming a problem.
The algorithm needs to load up an existing file, convert it to hex,
(then save that hex representation into a temp file) output that file
into a richtextbox, and then close. Now I have an algorithm that works
well, but it is not perfect. For instance it will add a series of F's
at the end of each line. Here it is:
//temporary file to write the hexadecimal interpretation to
FILE * tempFile;
tempFile = fopen("temphex","w");
int count=0;
char c;
try {
//To read the file
ifstream hexReader ((CString)hexName);
fprintf(tempFile, " ");
for(int i=0; i<16;i++) {
if(i == 7) {
fprintf(tempFile, "%02X ",i);
} else {
fprintf(tempFile, "%02X ",i);
}
}
fprintf(tempFile, "\n\n");
//While there is still data to read from the file, read it
//and then write to the temp file the hex interpretation
int line = 0;
fprintf(tempFile, "%08X ", line);
line++;
while(hexReader.good()) {
c = (char)hexReader.get();
if(count == 7) {
fprintf(tempFile, "%02X ",c);
count++;
} else if(count == 15) {
fprintf(tempFile, "%02X \n%08X ",c,line);
count=0;
line++;
} else {
fprintf(tempFile, "%02X ",c);
count++;
}
}
//Close the reader
hexReader.close();
}
Any help on fixing just the hex conversion part of the algorithm would
be appreciated.