473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Windows Forms File - Hex Conversion help.

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.

Jul 23 '05 #1
5 2529
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
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.

Jul 23 '05 #3
* 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
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?

Jul 23 '05 #5
* 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'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephan Lux | last post by:
Hi all, I have developed a windows service with Visual Basic .NET which starts a file conversion tool via the commandline. The tool open the files (z.B. Word, Excel, Acrobat...) and prints them...
8
by: Scott | last post by:
I have written windows applications using MFC for several years and have frequently used MFC techniques. Now I'm moving to C# .NET WinForm. Mostly C# books describes C# language (sometimes...
1
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm...
0
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server -...
7
by: G. Schmelzer | last post by:
Hi Ng, I've got a problem with the debugging of a Windows Application. I start the app in Debug Mode. But when an exception appears, vs.net doesen't jump to code line where the exception...
2
by: Budhi Saputra Prasetya | last post by:
Hi, I managed to create a Windows Form Control and put it on my ASP .NET page. I have done the suggestion that is provided by modifying the security settings. From the stack trace, I would...
0
by: Budhi Saputra Prasetya | last post by:
Hi, I still have the same problem with embedding Windows Control. I'll just requote what I posted last time: I managed to create a Windows Form Control and put it on my ASP .NET page. I...
2
by: sambo251 | last post by:
After running a few updates I get this very annoying "Windows Installer" error #1706 that will ne go away! It keeps saying that it cannot find the file "instantsharedevices.msi", that it is on...
3
by: shobhitguptait | last post by:
How to Run C#.NET Windows App on N/W with centralized DB using SQL SERVER 2000 Hello All...i m really grate full to c such a website where developers try to help people like us who face problems...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.