HexDump in C++

This is a discussion on HexDump in C++ within the C++ Programming forums, part of the General Programming Boards category; <<sponging continued from here http://cboard.cprogramming.com/showthread.php?t=83717 >> Hello every one.... can you help me out here... I need to make 2 ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    14

    HexDump in C++

    <<sponging continued from here HexDump Programm....>>

    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++ exemples 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;
    }
    Last edited by Kdar; 10-03-2006 at 08:09 AM. Reason: Snide continuation remark. [--Dave]

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >But now... How do I use hex table to get data file in to hex..
    Create a translation table:
    Code:
       char hex_table[16] =
          {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
       //Code here to read byte from file into byte
       //Output hex value of upper and lower nibble of byte
       cout << hex_table[upper(byte)] << hex_table[lower(byte)];

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Perhaps when you can post links and code properly (your image link is broken, and we use [code][/code] not [quote][/quote] tags).

    Does anybody bother to read any intro threads before diving right in and making an half-assed attempt?

    As for the problem, they're the same apart from replacing printf() in C with cout in C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Does anybody bother to read any intro threads before diving right in and making an half-assed attempt?
    Come on now, you really want us to answer that question seriously?

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    wtf are intro threads?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    Quote Originally Posted by swoopy
    >But now... How do I use hex table to get data file in to hex..
    Create a translation table:
    Code:
       char hex_table[16] =
          {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
       //Code here to read byte from file into byte
       //Output hex value of upper and lower nibble of byte
       cout << hex_table[upper(byte)] << hex_table[lower(byte)];
    so.. for "//Code here to read byte from file into byte"

    Use some thing like this?

    inData >> ...............

    outData << ............

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    ?....................

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Kdar
    so.. for "//Code here to read byte from file into byte"

    Use some thing like this?

    inData >> ...............

    outData << ............
    You can do that. You can also use read if you open the file in binary mode:
    Code:
    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));
    
    //Now loop to print upper and lower for each element of buffer

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    Some thing like 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;
    	
    	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));
    
    
    	while(true)
    	{
    		  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;
    		}
    	}
    	outData.open("name1.txt");
    	
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Some thing like this?
    Yes. To do the read, you have three different options depending on what in the file you want to print in hex. Firstly, if you want don't want whitespace as part of the hex dump, you can use:
    Code:
    inData >> ...............
    Secondly, if you do want spaces to be part of the hex dump, you can use getline
    Code:
    char line[200];
    inData.getline(line, sizeof(line));
    Thirdly, if you want whitespace and newlines as part of the hex dump, use read, as you've posted.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > Some thing like this?
    Yeah "something" like that.
    Did you compile it?

    First immediately obvious problem is lower() and upper() are declared inside main(). C++ doesn't have nested functions like say Pascal.

    Try it with a small buffer first, say
    char buffer[] = "hello world";

    before getting all messy and reading it from a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    Quote Originally Posted by Salem
    > Some thing like this?
    Yeah "something" like that.
    Did you compile it?

    First immediately obvious problem is lower() and upper() are declared inside main(). C++ doesn't have nested functions like say Pascal.

    Try it with a small buffer first, say
    char buffer[] = "hello world";

    before getting all messy and reading it from a file.

    ... Well it didn't compiled...

    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));
    	return = 0;
    }
    
    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;
    }
    I tryed this, and tryed just the char buffer[] = "hello world";..

    But it don't compile..

    Maybe I have put this part.... wrong into program..?

    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;
    }[/
    Sorry.. maybe I asking stupid questions.. but I never done any file openings and hex dump before..

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > return = 0;
    What does that do?

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    14
    Quote Originally Posted by swoopy
    > return = 0;
    What does that do?
    oops.. that return 0;

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    I dunno, maybe consider the possibilities of
    cout << lower(buffer[0]) << upper(buffer[0]);

    It won't work "as is", but with a bit of thought, you might get somewhere.

    The rest is just a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  2. HexDump Programm....
    By Kdar in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 06:13 AM
  3. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM
  4. Hexdump...?
    By ylph82 in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21