Hello, I am attempting to convert hex to an int.
It's good to get this stuff straight early on. Hexadecimal is merely one of many visual representations of an integer. The representation you're most familiar with is decimal (base10). 0x0A (hex) and 10 (decimal) are the same number -- the same 'int'.
I'm sure most people will understand what you mean, but your initial statement ("I am attempting to convert hex to an int") doesn't really make sense. Take some time to understand this, it will serve you well!
You appear to be using std::string to hold an array of bytes, rather than some text. While this is certainly do-able, the semantics of the word "string" in programming probably don't match up too well with what you're trying to do with the content of that container. I'd suggest switching to something like std::vector<unsigned char> instead.
Anyway, given an array of bytes, whether it be in a string, or a vector, or whatever else, the answer is to use memcpy:
#include <iostream>
#include <cstring>
#include <string>
int main()
{
const std::string s1 = "\x3F\xD0\x21\x4A";
const unsigned s2 = 0x4A21D03F; // NOTE: I'm on a little endian machine where sizeof(unsigned) == 4 and CHAR_BIT == 8. You probably are too
unsigned s1_as_int = 0;
std::memcpy(&s1_as_int, s1.c_str(), 4);
std::cout << (s2 == s1_as_int) << '\n';
return 0;
}
However! I suspect what you've done is read in data from a file in to a string and are subsequently attempting to convert it to properly typed and structured data.
If you have a "binary file", it might be easier to read it piece by piece in to ints and other datatypes. This assumes that the machine and compiler that wrote the file matches your machine and compiler, but if you provide a little more information some better targeted advice can be offered in this area, I'm sure. So what's your higher-level goal?