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) 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; }
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.
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; }
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)];
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?
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