String to DWORD

I have string variable (0x7515A0FA). Is it possible to convert in DWORD?
http://www.cplusplus.com/reference/cstdlib/strtol/

DWORD doubleWord = strtol("0x7515A0FA",0, 0);

or as a complete program,
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <windows.h>

int main()
{
    std::string input("0x7515A0FA");
    DWORD output = strtol(input.c_str(), 0, 0);
    std::cout << std::hex << output << std::endl;
}
Last edited on
Alternatively, with streams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <string>
#include <Windows.h>

int main(int argc, char* argv)
{
    DWORD doubleWord;
    std::string dwordHexString = "0x7515A0FA";
    std::stringstream dwordStream;
    dwordStream << dwordHexString;
    dwordStream >> std::hex >> doubleWord;
    std::cout << doubleWord;

    return 0;
}
Last edited on
closed account (3qX21hU5)
or a even shorter version ;p

1
2
3
4
5
6
7
8
9
10
int main()
{
    DWORD doubleWord;
    std::string dWordHexString = "0x7515A0FA";
    std::stringstream ss(dWordHexString);

    ss >> std::hex >> doubleWord;

    return 0;
}


But ya I would go with stringstream like booradley mentioned.
Topic archived. No new replies allowed.