Windows Forms File - Hex Conversion help.

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.
 
A

Alf P. Steinbach

* 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;

Why not use C++ streams?
try {
//To read the file
ifstream hexReader ((CString)hexName);

CString is not a standard type.

fprintf(tempFile, " ");
for(int i=0; i<16;i++) {
if(i == 7) {
fprintf(tempFile, "%02X ",i);
} else {
fprintf(tempFile, "%02X ",i);
}
}
Indentation.


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();
}

Indentation.

It sure _looks_ like unmatched braces here, but it's your job to check
that and fix it.

Any help on fixing just the hex conversion part of the algorithm would
be appreciated.

Please first fix the indentation.
 
T

TazCoder

Here is the fix with indentation. There were problems while trying to
post it that is why it looked the way it did. Here goes:


All inside the method:

{
.......//other stuff before this, not important
//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();
}
......//other stuff after that is not important to this part of the
problem
}

CString was just used, and the way the files are streamed is not
important. The files are created, edited, and then loaded into the
RichTextBox correctly so that is not the issue. (Remember this is
windows forms though) The entire program is depent on .Net framework
etc. so using CString is fine in this case. Thanks for any help given.
 
A

Alf P. Steinbach

* TazCoder:
Here is the fix with indentation. There were problems while trying to
post it that is why it looked the way it did. Here goes:


All inside the method:

{
......//other stuff before this, not important
//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);

Since you want to do this thing for "any file", I presume the file can
be a binary one. You should therefore specify the file as binary, to
avoid the automatic _translation_ that the C/C++ standard library does
for text files on e.g. Windows OS. Note: opening the file as binary
means that you'll see the characters that specify "new line" in the file.

std::ifstream hexReader( hexName, std::ios_base::binary );

fprintf(tempFile, " ");
for(int i=0; i<16;i++)
{
if(i == 7)
{
fprintf(tempFile, "%02X ",i);
}
else
{
fprintf(tempFile, "%02X ",i);
}
}

When you have a title row it's a good idea to think "format string".

Unfortunately the C++ standard library doesn't support that directly,
but you can emulate it by using a function that outputs a vector of
strings, one string per column, which it formats appropriately.

The rest seems OK (apart from I think you should really use a
formatting function); if there are further problems don't hesitate
to post them to the group!

Cheers,

- Alf
 
T

TazCoder

Any more help with this topic would be great! Once again, the problem
is that the printout of the hex would be correct until the end when it
would print out a stream of F's. (usually 8 of them.) Any reasons why?
Anyone know a better way to do this?
 
A

Alf P. Steinbach

* TazCoder:
Any more help with this topic would be great! Once again, the problem
is that the printout of the hex would be correct until the end when it
would print out a stream of F's. (usually 8 of them.) Any reasons why?
Anyone know a better way to do this?

The code doesn't check whether the call to 'get' succeeds or not.

If it doesn't, 'break'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top