HexDump

Oct 20, 2011 at 10:41pm
I have really no experience with Hex editing and such. I am working with this code I got from the internet to learn a little more.

When I run it, it gives me the Hex from the beginning of the file just fine, but when I enter my own offset, it gives me a bunch of CD CD CD's, after which it gives me the hex from the start of the file again. What am I doing wrong?

Here is the code:

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
35
36
int main()
{
    const char *filePath = "C:\\temp.hps"; 
    BYTE *fileBuf;          // Pointer to our buffered data
    FILE *file = NULL;      // File pointer
 
    // Open the file in binary mode using the "rb" format string
    // This also checks if the file exists and/or can be opened for reading correctly
    if ((file = fopen(filePath, "rb")) == NULL)
        cout << "Could not open specified file" << endl;

    else
        cout << "File opened successfully" << endl;

 

    // Get the size of the file in bytes
    long fileSize = getFileSize(file);
 
    // Allocate space in the buffer for the whole file
    fileBuf = new BYTE[fileSize];
 
    // Read the file in to the buffer
	streamoff offset = 0x00000080;
    fread((&fileBuf[offset]), fileSize, 1, file);
 
    // Now that we have the entire file buffered, we can take a look at some binary infomation
    // Lets take a look in hexadecimal
    for (int i = 0; i < 500; i++)
        printf("%X ", fileBuf[i]);
 
    cin.get();
    delete[]fileBuf;
        fclose(file);
    return 0;
}
Oct 21, 2011 at 2:43am
Your fread() isn't doing what you want. You are, instead of reading from the offset, simply passing an offset pointer to your array. So, say your offset was 3; you would start reading the file (from the beginning!) and storing it at buffer[3] and on. Since you are reading the entire file into memory, I would just start your for loop from the offset instead of 0.
Oct 21, 2011 at 9:42am
Thanks so much! Thanks for fixing my stupid mistake.

I have gotten a little farther with my program, and have hit another wall.

I now have no problem reading it where I want to, but writing it. Every time I try to open with fopen(filePath, "rb"), it has no problem, but "r+b" it gives me a stream!=NULL error.

Just to let you know about my program, when it is finished, it will eventually take in an offset in Hex, and a number for a calculation, use that number to calculate a hex value, and enter that Hex value into the offset(of the file) given by the user. Here is what I have so far.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

typedef unsigned char BYTE;


long getFileSize(FILE *file)
{

    long lCurPos, lEndPos;

    lCurPos = ftell(file);

    fseek(file, 0, 2);

    lEndPos = ftell(file);

    fseek(file, lCurPos, 0);

    return lEndPos;
}




int main(){
	double integer=0;		//used for calculation of Hex value
	int total;				//also used for calculation of Hex value
	const char *filePath = "C:\\temp.hps";
    BYTE *fileBuf;          // Pointer to our buffered data
    FILE *file = NULL;		// File pointer
	streamoff offset;


	cout<<"Please enter the offset in Hex:" <<endl;
	cin>>hex>>offset;
	cout<<"Please Enter how long the audio is in seconds:"<<endl;
	cin>>integer;
	total=32000*integer*(16.0/14.0)+15;

	//takes total and changes it into a hexidecimal string
	char Hex[9];
	_itoa(total, Hex, 16);

	// Open the file in binary mode using the "r+b" format string
    if ((file = fopen(filePath, "rb")) == NULL)
        cout << "Could not open specified file" << endl;
	else
        cout << "File opened successfully" << endl;

	string Hexy=Hex;
	cout<< "The appropriate hex is:"<<endl<<Hexy<<endl;

	cout<<"Now putting the hex into the correct offset."<<endl;

	//takes the string Hexy and puts it into an int that is usable.
	int x;   
	fstream ss;
	ss <<hex << Hexy;
	ss >>x;
	int *HexNum=&x;


    // Get the size of the file in bytes
    long fileSize = getFileSize(file);
 
    // Allocate space in the buffer for the whole file
    fileBuf = new BYTE[fileSize];
    
	// Read the file in to the buffer	
	fread(fileBuf, fileSize, 1, file);

	//go to the given offset and write the calculated Hex value there
	fseek(file, offset, SEEK_CUR);
	fwrite(HexNum, 1, 8, file);

	//just printing to make sure it is at the right offset
    for(int i = offset; i < offset+500; i++)
	{
        printf("%X ", fileBuf[i]);
	} 
    cin.get();
	delete[]fileBuf;
 	fclose(file);
	

	system("pause");
	


}



Any tips/ problems?
Topic archived. No new replies allowed.