0

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++?

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
  • 1
    You 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 Answers2

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
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