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.