Need to read in a DWORD and compair it to another

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
A DWORD is a double word, so it's 32 bits... So read 4 bytes in from the file stream and cast it and you should be golden.
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
how do I read bytes? None of my books cover this they all are about converting bin to hex or decimal with no explaination on how to manipulate the bytes themselves.
Thanks
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Go Google C++ stream i/o. You need to determine the path to the file, open a stream on it, read an int (or an unsigned int) from the stream, close the stream and then perform the comparison. Don't forget error handling.
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
any kind of file, I have given up on trying it in binary, I want to edit it in hex now(I am good with hex editors but want to make an app that does this similar to a hex editor)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
A hex editor needs to read, modify and write to binary files. EXE and DLL files are binary even though the editor displays them as hex.

If you poke around CodeProject.com or try Googling "hex editor source code c++" you might find an existing project to look at.
 

iCyborg

Golden Member
Aug 8, 2008
1,337
59
91
DWORD is just a typedef for some integer type, you read it in the exact same way as you read any other builtin types (integers, floats, chars etc) from a file.
Are you asking us how to read anything from a file in c++? You should just google fstream/ifstream for that like Mark said. Not sure what the problem is; the difference between reading a value into an integer variable x and reading into a DWORD x would be to change declaration 'int x;' into 'DWORD x;', fstream's operator>> takes care of types, am I missing something here?
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
what I am trying to do is read in four bytes and see if they are <= 07070707(hex)
then write them to another file would I have to have it write 0x07 0x07 0x07 0x07?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
what I am trying to do is read in four bytes and see if they are <= 07070707(hex)
then write them to another file would I have to have it write 0x07 0x07 0x07 0x07?

You're missing some fundamental concepts. For the moment, stop thinking in terms of hexadecimal values. What gets written to/read from a file is bytes. Each byte has 8 bits and can represent values from 0..255. Any given number of bits can represent values from 0 .. 2^n - 1, where n is the number of bits. While most programming languages provide low-level datatypes that represent a specific number of bits (i.e. the byte or word datatypes for 8 or 16/32 bit values), it is more common to use numeric datatypes for manipulating scalar values in a program, i.e. in C++ on a 32-bit architecture:

unsigned short int = 16 bits = (2^16 - 1) = 65535 = 0xffff
short int = 15 bits = (2^15 - 1) = 32767 = 0x7fff

unsigned int = 32 bits = (2^32 - 1) = 4294967295 = 0xffffffff
int = 31 bits = (2^31 -1) = 2147483647 = 0x7fffffff

unsigned long = 32 bits = (2^32 - 1) = 4294967295 = 0xffffffff
long = 31 bits = (2^31 -1) = 2147483647 = 0x7fffffff

All numeric values are stored and manipulated as binary numbers at runtime. The difference between signed and unsigned representations is that signed representations reserve the high bit for the sign (positive/negative). Therefore the maximum positive value they can represent is half the unsigned version. Regardless, it's all some number of bits: decimal and hexadecimal representations are meaningless to the computer.

unsigned short int i = 65535;
unsigned short int j = 0xffff;

Both of these definitions create a binary unsigned integer with 16 bits all set to 1.

unsigned int k = i + j;

Here I add the two unsigned binary short ints to initialize an unsigned binary int. Note, again, all values are stored as binary numbers. A decimal or hexadecimal representation of the numbers is only relevant when I want to either print the values to the screen, a printer, or write them to a file _as text_ (or use literals to initialize variable as above).

cout << hex << k << endl;
cout << oct << k << endl;
cout << k << endl;

This code uses the standard console output stream instance cout to write the same binary value to the console in hexadecimal, octal, and decimal formats. It prints...

0x1fffe
0377776
131070

Now back to files. I mentioned above that you are always writing bytes to a file. That's more or less true, but C++ makes it easier to read and write the native data types and even structs and classes to a file and read them back. You can open a stream on a file and use syntax similar to the example above, so something like this...

Code:
#include <iostream>
#include <fstream>

int main() {
    ofstream myfile;
    myfile.open("examplefile.bin", ios::binary);
    int k = 0x7fffffff;
    myfile << k;
    myfile.close();
}

This code writes the binary value in k to the file examplefile.bin. Regardless of how I think about the value in k, whether it is decimal, hexadecimal, etc., the same bits get written to the file. This code, however, does something different...

Code:
#include <iostream>
#include <fstream>

int main() {
    ofstream myfile;
    myfile.open("examplefile.txt");
    int k = 0x7fffffff;
    myfile << k;
    myfile.close();
}

In this case the file was opened as text, and while my C++ is a bit rusty I believe what it will do is write the characters that represent the decimal expression of this value to the file, in other words the string "2147483647". The first example writes four bytes (a 32-bit machine word) to the file example.bin. The second example writes 10 or 20 bytes to the file example.txt, depending on how characters are encoded. What if I want the text file to contain the hexadecimal representation?

Code:
#include <iostream>
#include <fstream>

int main() {
    ofstream myfile;
    myfile.open("examplefile.txt");
    int k = 0x7fffffff;
    myfile <<  hex << k;
    myfile.close();
}

Again, my C++ is rusty, but that should write the string "0x7fffffff" to the file, again requiring 10 or 20 bytes depending on encoding.

So the key point of all this is that you need to start thinking in terms of values and performing operations on variables containing values. If you want the user to see those values as hex, then you can convert input and output to a hexadecimal representation, but you'll always be working with binary numbers in your program.
 

Schmide

Diamond Member
Mar 7, 2002
5,670
894
126
Regardless of reading and writing you could use a union struct to avoid some of the casting to get at the bytes of a dword

Code:
struct	sFloatIntChar
{
	union
	{
		float		m_float;
		int		m_int;
		unsigned int	m_uint;
		char		m_char[4];
		unsigned char	m_uchar[4];
	};
};

So using it

Code:
    sFloatIntChar sData;
    sData.m_uint=0x01020304;
    myFile << hex << sData.m_uchar[0]; 
    myFile << hex << sData.m_uchar[1];
    myFile << hex << sData.m_uchar[2];
    myFile << hex << sData.m_uchar[3];
    myFile.close();

Or you can do funky little casts like so.

Code:
	int uInt=sData.m_int;
	sData.m_uchar[0]=((unsigned char *)&uInt)[3];
	sData.m_uchar[1]=((unsigned char *)&uInt)[2];
	sData.m_uchar[2]=((unsigned char *)&uInt)[1];
	sData.m_uchar[3]=((unsigned char *)&uInt)[0];
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
Thankyou so much for the informative replies. I read through it once and am digesting it(it takes me a little longer to get it than other people), I shall read it again in a couple of hours and see if it clicks.
On one of your lines you declared int k then assigned it a value in hex. would it still be an int if it was letters?
And would a DWORD char do the same?
 
Last edited:

exdeath

Lifer
Jan 29, 2004
13,679
10
81
DWORD is type "unsigned long int" in C++ on x86.

Bytes are bytes. It doesn't matter what they are, or what they are called, it's what you do with them that matters. A byte can be a numerical variable, a ASCII character, the amplitude of a sound wave for 1/44,100th of a second in 8 bit 44.1 KHz raw uncompressed audio data, the intensity of the 8 bit blue component of a 32 bit RGBA pixel, or a CPU instruction. The value of a single byte of data only means what it's interpreted to mean by program or hardware, the only hard rule about that byte is that it's 8 bits and thus can only differentiate itself between 256 unique values.

To make it simpler with plain C:

#define DWORD unsigned long int

DWORD a;

fread (&a, sizeof(DWORD), 1, file);

if(a==0x12345678)

...

a==16 is the same thing as a=0x00000010, etc.

A char is not a DWORD, a char is a single byte. char b = '0', then BYTE b == 0x30. If you cast a unsigned long int (DWORD) you truncate the upper 3 bytes and only return the lowest byte as a char.

char b;
b = '0'; // ASCII
b = (char)0x30 //hex
b = (char)48; //decimal

All of these result in the same exact value stored in b.

It's all the same. Text is just numerical bytes and byte value determines the character, and it's entirely arbitrary and by established convention what byte value is what character, but we typically use the well known and defined ASCII definition.

ascii-table1.gif


char c = 101;

cout << c; //results in "e" printed to the screen.

c = 0x3D;

cout << c; //results in "=" printed to the screen.

c = '3';

cout << c; //results in "3" printed to the screen.
 
Last edited:

Paul98

Diamond Member
Jan 31, 2010
3,732
199
106
Thankyou so much for the informative replies. I read through it once and am digesting it(it takes me a little longer to get it than other people), I shall read it again in a couple of hours and see if it clicks.
On one of your lines you declared int k then assigned it a value in hex. would it still be an int if it was letters?
And would a DWORD char do the same?

int, hex, binary, char,... all are just different representations of the same thing.

the int value of 15, it's exactly the same as f in hex. It's like saying 2 is exactly the same as 1+1. Just different ways of representing a number.

You might want to look into type casting you can see how different things are represented.
 

Paul98

Diamond Member
Jan 31, 2010
3,732
199
106
As for writing to a file, you seem to think you need to change everything to hex in your code then write it to the file. If you were to write the text "16" to a file if you were to look at it in a hex editor you would see 3136. Since 31 is the char 1, and 36 is 6.

Now if you were to write the value of 16 to the file it would be the exact same as writing 10 in hex to the file. And if you were to open up that file in a hex editor you would see 10. Now it also depends on the size in bytes of what you are using. So depending on it's size it might write 10, 0010.

So you can read from the file in int, short,... what ever you want and then make adjustments on those values then write it back to the file. You will get exactly the same results if you were to adjust it as hex.