Reputation: 174
I'm trying to read and write hex but I'm having trouble inputting hex and reading hex back as hex not ascii. Whats getting me is printing the addresses, and values correctly I'm not quite sure I'm doing it right. any hints as to what I'm doing wrong? ok its working so far now just to fix read to print the actual address instead of entered address +1.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main(int argc,char ** argv) {
if(argc <= 1) {
cout<<"Enter a file name please";
exit(0);
} else {
fstream in;
in.open(argv[1],fstream::in | fstream::out | fstream::binary);
string input;
unsigned int v;
unsigned int print;
unsigned int g;
int iter;
for(iter = 0; input!="save";){
cout<<"Hex Edit("<<argv[1]<<"): ";
cin>>input;
if(input == "read"){
cout<<"Enter Offset: ";
cin>>hex>>v;
in.seekg(v);
print=in.get();
g=in.tellg();
cout<<"Value at offset("<<hex<<g<<"): "<<hex<<print;
cout<<endl;
}
if(input == "write"){
cout<<"Enter Offset: ";
cin>>hex>>v;
in.seekp(v);
cout<<"Enter Value: ";
cin>>hex>>v;
in.put(v);
}
} else if(input == "save") {
in.close();
}
cout<<endl;
}
}
return 0;
}
Upvotes: 1
Views: 8358
Reputation: 91
You seem to be intending to read and write one character at a time - cin and cout are both capable of working with integral values and this would be ideal here. Keep in mind that hexadecimal strings do represent numbers! Streams are capable of interpreting them as such. Simply changing the variables v and print to type int will allow you to read in an entire hexadecimal value at once.
For an example:
int value;
cin >> hex >> value;
cout << hex << value;
Upvotes: 2