c++ - 用 C++ 输入十六进制和读取十六进制


#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;
}

时间:

记住十六进制字符串表示数字!只需更改变量v并打印为int类型,就可以一次读取整个十六进制值。

例如:


int value;
cin >> hex >> value;
cout << hex << value;

...