0

I'm making a project that dumps a file by putting the offsets (hexadecimal). I must put the hexadecimal with 0x like 0xBEE4DC because the application will crash or return an error if I put a hexadecimal without 0x like BEE4DC. Is there a way to put the hexadecimal without "0x" ?

This is my code. i'm keeping this project secret so I don't show more code than this.

dump1.cpp

void LoadMetadata(char* szFile)
{
    string mystr;
    int offset2;
    int offset1;
    std::cout << "Input unknown offset #1: ";
    getline(cin, mystr);
    stringstream(mystr) >> offset1;
    std::cout << "Input unknown offset #2: ";
    getline(cin, mystr);
    stringstream(mystr) >> offset2;
    std::cout << "\n";

    ...

}

dump2.cpp

static int offset2;
static int offset1;

void LoadDumpLib(char* szFile)
{

...
    pCodeRegistration = (DumpCodeRegistration*)MapVATR(offset2, pLibIl2Cpp);
    pMetadataRegistration = (DumpMetadataRegistration*)MapVATR(offset1, pLibIl2Cpp);

...
}
  • You can read your hex number (without "0x" at the beginning) as a string and then parse string (check whether it has "0x" or not and then add "0x" before next operations if needed) – gravell Oct 20 '16 at 10:02

3 Answers3

0

The following code save the number in hex without 0x and load it.

string mystr;
int offset1;
std::cout << "Input unknown offset #1: ";
getline(cin, mystr);
stringstream ss( mystr);
ss << std::hex;
ss >> offset1;
std::cout << offset1 << std::endl;

std::string str2;
stringstream s2;
s2 << std::hex << 0x2123;
s2.flush();
s2 >> str2;
std::cout << str2 << std::endl;
sanjay
  • 735
  • 1
  • 5
  • 23
0

This code should work

std::cout << "Input unknown offset #1: ";
getline(cin, mystr);
stringstream(mystr) >> hex >> offset1;
std::cout << "Input unknown offset #2: ";
getline(cin, mystr);
stringstream(mystr) >> hex >> offset2;
std::cout << "\n";
0

Within code, a literal value needs the 0x prefix to be interpreted as hex. So the 0x is not optional in something like.

  unsigned x = 0xABC;

When reading a value, for example using streams, the 0x is not required (in fact, if you want the prefix, it is necessary to code to handle it). So, for example, using a std::istringstream to read data from a std::string;

  std::string x("ABC");
  std::istringstream sx(x);
  unsigned v;

  sx >>  std::hex >> v;

  if (v == 0xABC) std::cout << "Equal\n";
Peter
  • 35,646
  • 4
  • 32
  • 74