Write hex in text file to other file

Pages: 12
I need some help getting hex from a text file and writing it to a different file.
I made a program to dump hex from files into a text file and now I need it to save the hex to the original file. Here is my code to save the hex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main(int argc, char * argv[]) {
	ofstream myFile;
	ifstream file(argv[1], ios::in);
	myFile.open(argv[2], ios::out | ios::binary);
	char buffer[BUFFER_SIZE];
	char temp;
	int len = sizeof(buffer);
        if(file.is_open())
        {

		for(int i = 0;!file.eof(); ++i)
                {
			buffer[i] = '0x';
                        file >> temp;
			buffer[i] += temp;
			temp = NULL;
			
		}
        }
	file.close();
	cout<<buffer;
	myFile.write(buffer, BUFFER_SIZE);
	
	myFile.close();
	return 0;

}

It never outputs the correct contents of the file that has been dumped. I've checked with other hex editors and the hex dumper is working correctly.
Last edited on
can you please post a snippet of data of the input hex text file.
These are the first few lines:
 58 45 58 32 00 00 00 01 00 00 10 00 00 00 00 00
 00 00 00 70 00 00 00 0B 00 00 03 FF 00 00 02 3C
 00 01 01 00 92 00 23 38 00 01 02 01 92 00 00 00
 00 01 03 FF 00 00 0E 14 00 01 80 02 00 00 02 60
 00 01 83 FF 00 00 02 68 00 02 00 FF 00 00 02 78
 00 02 01 04 00 00 02 DC 00 02 02 00 00 01 00 00
 00 04 00 06 00 00 02 EC 00 04 04 04 00 00 03 04
 00 00 01 CC 00 00 80 00 F4 81 32 C1 21 81 B4 4F
 4D 82 78 2A 74 1B E8 2D B3 A5 D3 D6 30 4C A8 69
 3B C1 20 D9 B7 65 DA C6 02 C8 80 BD 35 F9 A2 0B
 29 A3 C7 51 77 7F DB 68 96 DE 84 0C DF 2E 26 A0
 83 1F E4 8B 1D 72 6A 79 6F 74 EC 5B 31 A5 62 BC
 51 A9 37 62 51 1E 56 0B 98 F0 3C 4C A3 17 42 1F
Last edited on
given the structure of your input file, you should be reading each 2 digit string from the input file and converting string value read to corresponding byte value. you can accomplish this with:

1
2
3
4
int HexByteStringToInt(const string & strHB)
{
   return (strHB[0]-'0')*16 + strHB[1]-'0';
}


does this help?
Last edited on
@fabtasticwill

I'm not sure how to correct it, but the main problem you're having is that each character is being read separately into buffer[i]. IE: First number, 58, is being read as 5 then the next buffer[i] reads the 8, since char only holds one character.
Last edited on
Thanks, but how exactly would I use this? I tried using that on the buffer, but that didn't help.
Last edited on
Thanks for the reply. I tried using char * instead of char but it said temp was uninitialized, even though it was being read from a file.
within your file reader section, try reading each word into a string, ie, from your input snippet the first word read into say string strHexByte would be "58"

the next would be "45", ...

each time you read a hex byte you should pass it to function HexByteStringToInt.

this will convert the hexadecimal byte value stored in [strHexByte] to the corresponding integer value.

for example: HexByteStringToInt("58") = 88.

you then place 88 inside of your buffer and advance the index.

once done you should be able to write the buffer to file and end up with a copy of the original binary.
I just tried that but when I run it, it says the program stopped working and closed. I'll give you the code i'm trying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int HexByteStringToInt(const string & strHB)
{
   return (strHB[0]-'0')*16 + strHB[1]-'0';
}

int main(int argc, char * argv[]) {
	ofstream myFile;
	ifstream file(argv[1], ios::in);
	myFile.open(argv[2], ios::out | ios::binary);
	char * buffer[BUFFER_SIZE];//When I did it with char it worked correctly with this. = {0x58, 0x45, 0x5, 0x32, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00};
	char * temp = "  ";
	int len = sizeof(buffer);
        if(file.is_open())
        {

		for(int i = 0;!file.eof(); ++i)
               {
                        file >> buffer[i];
			buffer[i] = (char *)HexByteStringToInt(buffer[i]);
		
			
		}

	
        }
	file.close();


	myFile.write((char *)buffer, BUFFER_SIZE);
	
	myFile.close();
	return 0;

}
Last edited on
Ok, I just got it working, but i've got one problem. The output file has spaces between all the characters. I need to get rid of that. heres my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(int argc, char * argv[]) {
	ofstream myFile;
	ifstream file(argv[1], ios::in);
	myFile.open(argv[2], ios::out | ios::binary);
	int buffer[BUFFER_SIZE];
        if(file.is_open())
        {
		for(int i = 0;i < BUFFER_SIZE; ++i)
                {
                     file >>hex>> buffer[i];			
		}
        }
	file.close();

	myFile.write((char *)buffer, BUFFER_SIZE);
	
	myFile.close();
	return 0;

}
Last edited on
don't read from file into buffer, instead try reading from file into a string such that you get a two character string each time before you pass value to the HexByteStringToInt function.

don't read one byte out of your input file at a time (unless you build in the logic to combine characters to form the require hex byte strings.

change line 18 from
 
file >> buffer[i];


to:

1
2
string strHB;
file >> strHB;

I'm not using HexByteStringToInt anymore. I got it working without it. My problem now is that it adds spaces between all characters in the output file.
Last edited on
you should then try my suggestion
You mean using a string instead of and int? I tried and it didn't work and i'm not sure why. Here's the code I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(int argc, char * argv[]) {
	ofstream myFile;
	ifstream file(argv[1], ios::in);
	myFile.open(argv[2], ios::out | ios::binary);
	int buffer[BUFFER_SIZE];
	char * result;
        if(file.is_open())
        {
		for(int i = 0;!file.eof(); ++i)
                {
                        file>>hex>> result[i];
			cout << hex << result[i] << ' ';
		}
        }
	file.close();
	myFile.write(result, BUFFER_SIZE);
	
	myFile.close();
	return 0;

}
Last edited on
my suggestion was to use a stl string.

so line 6 should become:

 
string result;


also remove the [hex] keyword from your file read operation.

also ensure that you include the correct stl files:

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

notice the absence the the [.h] extension.
When I tried that, it outputed
 
ØîZ 2A ÌÌÌÌÌÌÌÌÌÌÌÌÌ      ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ

there are a lot more of the Ì than I put. I used cout to look at the string and it had the hex. I think the problem was converting it to char *.
Last edited on
the line:

 
myFile.write(result, BUFFER_SIZE);


should be

 
myFile.write(result, actual_byte_cnt);


where actual_byte_cnt is the actual number of bytes converted instead of the total size of your output buffer
Thanks, but I know that. I was just using BUFFER_SIZE as a temporary solution. Thats where all the Ì come from. Right now all I need is to remove the extra spaces.
don't define buffer as an array of type [int] instead define it as an array of type char, like:

 
char buffer[BUFFER_SIZE];


an int is usually defined as 4 bytes in length whereas a char is define as having length of 1 byte.

this might be where the extra spaces/junk in between is coming from.
Thanks, I'll try it and see if it works.
Last edited on
Pages: 12