Windows Forms File - Hex Conversion help.

Discussion in 'C++' started by TazCoder, May 26, 2005.

  1. TazCoder

    TazCoder Guest

    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.
    TazCoder, May 26, 2005
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, May 26, 2005
    #2
    1. Advertising

  3. TazCoder

    TazCoder Guest

    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.
    TazCoder, May 26, 2005
    #3
  4. * 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?
    Alf P. Steinbach, May 26, 2005
    #4
  5. TazCoder

    TazCoder Guest

    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?
    TazCoder, May 30, 2005
    #5
  6. * 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?
    Alf P. Steinbach, May 30, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,109
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    439
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    565
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,764
  5. tim
    Replies:
    2
    Views:
    1,522
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page