1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Hex Dump in C and C++

Discussion in 'C++' started by Kdar, Oct 3, 2006.

  1. Kdar

    Kdar New Member

    Joined:
    Oct 3, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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)

    [​IMG]

    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;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.
     
  3. Kdar

    Kdar New Member

    Joined:
    Oct 3, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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)];
    
     
  5. Kdar

    Kdar New Member

    Joined:
    Oct 3, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  6. Kdar

    Kdar New Member

    Joined:
    Oct 3, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    ........?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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
     

Share This Page