string to integer

Q

quickcur

If I have a string in Hex, e.g., "0xff", how can I turn it into
numbers?

Thanks,

qq
 
A

Alan

If I have a string in Hex, e.g., "0xff", how can I turn it into
numbers?

long strtol( const char *s, char **endptr. int radix) in stdlib.h

If radix is zero and first char is "0" and second is "x" (or "X"), string is
interpreted as hexadecimal.

long num = strtol("0xff", NULL, 0);
 
R

Robert Swan

If I have a string in Hex, e.g., "0xff", how can I turn it into
numbers?

if none of the ios_base::hex/dec/oct are set on a std input stream then
the prefix determines the base for string formatting

#include <iostream>
#include <string>
#include <sstream>

int main() {
std::string n_string("0xff");
std::istringstream n_stream(n_string);
n_stream.unsetf(std::ios_base::dec);

int result;
n_stream >> result;
std::cout << result << std::endl; // should output 255
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top