tags:

views:

384

answers:

1

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

+6  A: 

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";
sbi
+1 for "dammit!" ;)
Georg Fritzsche
Well, that was awesome. But I didn't need it in decimal, I needed it in binary. Anyway, I researched a bit and did the improvisation.. :) Here : http://pastebin.com/m401f1b79
halluc1nati0n
@halluc1nati0n: I would advice you to be more precise in your questions wording. Any variable is actually binary, and after `iss >> std::hex >> i` the value is actually in binary form. Now, what you were requesting is converting to a string representing the binary form, which is a different story. I am sure you would have gotten the result in the first place had you asked the appropriate question.
David Rodríguez - dribeas
@dribeas : I agree that it was a 'poor choice of words' on my part, but it did help me do my own improvisation on the code. Which is what Stack Overflow epitomizes, in a way. And moreover, I've already acknowledged this by choosing what 'sbi' gave as the 'correct' answer!
halluc1nati0n