Currently I got a trouble about how to convert a DWORD (unsigned int) value from a string to a regular integer value. I could not find anything which is related to string -> DWORD -> int -> char etc... Atoi & atof can't help me because they can't do this, they don't understand the hex value, Here's the example :
char *dw = "0xFDE390";
unsigned int dword = Convert(dw);
Is there any trick, method or function which can do this?
I'm looking forward to your help.
unsignedlongint strtoul ( constchar * str, char ** endptr, int base );
With strtoul you can also pass 0 as the base (the last param). strtoul (but not stoul) will then treat anything with a 0x prefix as hex. (The default for base for stoul is 10, but I haven't tried passing 0 to it...)
constchar* dec = "123";
const std::string hex = "0xFDE390";
// use 0 as base to strtoul works base out for itself
unsignedlong x = std::strtoul( dec, NULL, 0 );
unsignedlong y = std::strtoul( hex.c_str(), NULL, 0 );
// as stoul takes a const std::string&, rather than a const char*,
// there is no need for c_str() if it's used instead of strtoul
std::cout << std::showbase;
std::cout << "x = " << x << std::endl;
std::cout << std::hex << "y = " << y << std::endl;
x = 123
y = 0xfde390
PS There's no cplusplus.com entry for stoul at the mo, but there is this:
You can also take this as a programming challenge, and try to workout your own function that can convert a hexadecimal string into a unsigned int. That will give you some experience with handling strings.