I have a program that needs to take a hexadecimal value of 2 digits (up to FF
) and convert each digit into a separate 4-digit binary value (up to 1111
). I'm able to write an algorithm that would do this for me, but before I do that, is there an easy way to do this with built-in functions in C++?
Asked
Active
Viewed 120 times
0

Mension1234
- 99
- 1
- 10
-
Are doing this with string representations of hexadecimal characters? What is the type of `FF` – asimes Nov 05 '16 at 18:15
-
1You can perhaps check if the below answers your question. http://stackoverflow.com/questions/18310952/convert-strings-between-hex-format-and-binary-format – Soundararajan Nov 05 '16 at 18:15
2 Answers
0
Write a mapping between the hex charachters an their binary.
std::string HexCharToBinary( char c ) {
switch (c) {
case '0': return "0000";
case '1': return "0001";
// Now input rest of the cases.
case 'f': return "1111";
default: assert(false); return "bad input";
};
As pointed out @Crazy Eddie in comment section.
Using an array would much better and optimal solution.

Shravan40
- 8,922
- 6
- 28
- 48
-
2Can you please link this (http://stackoverflow.com/questions/483609/how-can-i-convert-hexadecimal-numbers-to-binary-in-c) instead, to avoid duplication. – Soundararajan Nov 05 '16 at 18:17
-
-
If an answer to another question answers this, mark the question as duplicate rather than copy the answer. Definitely don't copy without attribution. – interjay Nov 05 '16 at 18:27
-
0
There's a similar question that was already answered here:
Convert strings between hex format and binary format
You might want to check it out, I think the answer provided there is complete and easy to understand.

Community
- 1
- 1

cookie_monster
- 1
- 2