Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Hex Dump in C and C++ (http://www.go4expert.com/forums/hex-dump-c-cpp-t1531/)

Kdar 3Oct2006 19:51

Hex Dump in C and C++
 
Hello every one.... can you help me out here...
I need to make 2 programms.. on C and other same C++

I need to make a programms that read binary file (like txt file) and produce a hexadecimal dump of that file.

output should look like this (out put should be stored in new txt file)

http://img239.imageshack.us/img239/9...titled1ua6.jpg

Also command line should be used to get the name of data file. and then program will use a lookup table to convert the binary byte to a hex character. Using printf is disallowed.

Here is C++ example I am trying to get working..

In C++, I was able to open file.. and write some to it.. but.. how do I change it to hex?

Here what I did in C++

This programm just drive me nuts..... plz help.

Code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

        string fileName;
        ifstream inData;
        ofstream outData;
        cout << "Enter the input file name: ";
        cin >> fileName;
        inData.open(fileName.c_str());
        outData.open("name1.txt");
}

That file enty part...


Here is how I can write some thing using original file content.. and change it and write it to new file...
Code:

        string socialNum;
        string firstName;
        string lastName;
        string middleName;
        string initial;



        inData >> socialNum >> firstName >> middleName >> lastName;

        initial = middleName.substr(0, 1) + '.';


        outData << firstName << ' ' << middleName << ' ' << lastName
                        << ' ' << socialNum << endl;
        outData << lastName << ", " << firstName << ' ' << middleName
                        << ' ' << socialNum << endl;
        outData << lastName << ", " << firstName << ' ' << initial
                        << ' ' << socialNum << endl;
        outData << firstName << ' ' << initial << ' ' << lastName;

        inData.close();
        outData.close();
        return 0;


But now... How do I use hex table to get data file in to hex..

Code:

}
unsigned char lower(unsigned char c)
{
        unsigned char one_byte;
        one_byte = c & 0x0F;
        return one_byte;
}
unsigned char upper(unsigned char c)
{
        unsigned char one_byte;
        one_byte = (c & 0xF0) >> 4;
        return one_byte;
}


shabbir 3Oct2006 20:03

Re: Hex Dump in C and C++
 
As you cannot use printf the only solution is to convert the decimal equivalent of the number into the HEX and save it to the file.

Kdar 3Oct2006 22:03

Re: Hex Dump in C and C++
 
Yes... I have to save it to new file...

Like file1.txt is original file with some test.

and file2.txt.. will hold the hex dump..

But any one have idea how would I conver binary to hex. and then save it..

to save stuff to file.. I probably will have to use.. something like this...

Code:

        outData << ......;
        outData << ...........;
        outData << ...................;
        outData << ............;

        inData.close();
        outData.close();

But how do I use this code? to get Hex from nibbles...
(which was given for me in assignment...)
Code:

}
unsigned char lower(unsigned char c)
{
        unsigned char one_byte;
        one_byte = c & 0x0F;
        return one_byte;
}
unsigned char upper(unsigned char c)
{
        unsigned char one_byte;
        one_byte = (c & 0xF0) >> 4;
        return one_byte;
}


shabbir 4Oct2006 10:04

Re: Hex Dump in C and C++
 
Here is the code to convert the data into Hex
Code:

//--- Print hex with leading zeros of the decimal value n
for (int i=2*sizeof(int) - 1; i>=0; i--)
  cout << "0123456789ABCDEF"[((n >> i*4) & 0xF)];


Kdar 4Oct2006 23:33

Re: Hex Dump in C and C++
 
I tried this

Code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

        string filename;
        ifstream inData;
        ofstream outData;
        cout << "Enter the input file name: ";
        cin >> filename;
        outData.open("name1.txt");
       
        ifstream inData(filename, ios::binary);
        if (!inData.is_open())
        {
          cout << "Unable to open file: " << filename << endl;
          return 1;
        }
       
        char buffer[1000];
        inData.read(buffer, sizeof(buffer));
}
for (int i=2*sizeof(int) - 1; i>=0; i--)
  cout << "0123456789ABCDEF"[((n >> i*4) & 0xF)];
unsigned char lower(unsigned char c)
{
        unsigned char one_byte;
        one_byte = c & 0x0F;
        return one_byte;
}
unsigned char upper(unsigned char c)
{
        unsigned char one_byte;
        one_byte = (c & 0xF0) >> 4;
        return one_byte;
}

But it don't compile..

What do I do wrong?

Kdar 5Oct2006 00:39

Re: Hex Dump in C and C++
 
........?

shabbir 5Oct2006 09:33

Re: Hex Dump in C and C++
 
There are quite a few errors which you should be rectifying.

1. inData variable is defined more than once
2. You have defined the for loop of the hex conversion outside the main function.

Thanks
Shabbir


All times are GMT +5.5. The time now is 06:39.