halluc1nati0n
halluc1nati0n

Reputation: 923

Convert from hexadecimal to binary C++

This kind of builds up on Already asked question... However here, say, I'm given a hexadecimal input which could be a max of '0xFFFF' I'll need it converted to binary, so that I'd end up with a max of 16 bits.

I was wondering if using 'bitset' it'd be quite simple.. Any ideas?

EDIT :

After getting answers, improvised piece of code here : http://pastebin.com/f7a6f0a69

Upvotes: 1

Views: 6398

Answers (1)

sbi
sbi

Reputation: 224159

Supposing by "hexadecimal input" you mean a string containing a hexadecimal number, then this would work:

const char* const str = "0xFFFF";
std::istringstream iss(str);
int i;
iss >> std::hex >> i;
if(!iss && !iss.eof()) throw "dammit!";
std::cout << '"' << str << "\": " << i << "(0x" << std::hex << i << ")\n";

Upvotes: 6

Related Questions