10

Is there a simple way to convert a binary bitset to hexadecimal? The function will be used in a CRC class and will only be used for standard output.

I've thought about using to_ulong() to convert the bitset to a integer, then converting the integers 10 - 15 to A - F using a switch case. However, I'm looking for something a little simpler.

I found this code on the internet:

#include <iostream>
#include <string>
#include <bitset>

using namespace std;
int main(){
    string binary_str("11001111");
    bitset<8> set(binary_str);  
    cout << hex << set.to_ulong() << endl;
}

It works great, but I need to store the output in a variable then return it to the function call rather than send it directly to standard out.

I've tried to alter the code but keep running into errors. Is there a way to change the code to store the hex value in a variable? Or, if there's a better way to do this please let me know.

Thank you.

navig8tr
  • 1,724
  • 8
  • 31
  • 69
  • So what you're really asking is.. how to get the hexadecimal representation of some `unsigned long` into a string? – Lightness Races in Orbit Oct 19 '13 at 01:53
  • 1
    I'm really just asking for a relatively simple way of converting a bitset to a hexadecimal value and then returning the value to the function call. – navig8tr Oct 19 '13 at 02:06
  • 1
    "I'm really just asking for a relatively simple way of converting a bitset to a hexadecimal value and then returning the value to the function call." In what form would you like to return that hex value? Wouldn't `std::string` work? Did you try my answer? Did it do what you were hoping to do? – Sergey Kalinichenko Oct 19 '13 at 02:48
  • @navig8tr: No, you're not. There is no internal representation for "hexadecimal values". Hexadecimal values [for all intents and purposes] only exist within string representations (and literals in your source code). This is the same for decimal values, actually. So if you want to "get" a hex value, you either mean you want a string or you misunderstood your own requirements. – Lightness Races in Orbit Oct 19 '13 at 18:03
  • And what do you mean by "return it to the function call"? – Lightness Races in Orbit Oct 19 '13 at 18:03
  • I just meant return to where the function was called. – navig8tr Oct 19 '13 at 18:38

3 Answers3

9

You can send the output to a std::stringstream, and then return the resultant string to the caller:

stringstream res;
res << hex << uppercase << set.to_ulong();
return res.str();

This would produce a result of type std::string.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This works great. Thanks dasblinkenlight. Now I just need to convert the lower case letters to upper case. – navig8tr Oct 19 '13 at 18:39
  • @navig8tr [This answer](http://stackoverflow.com/a/735215/335858) tells you how to do it. – Sergey Kalinichenko Oct 19 '13 at 19:58
  • 2
    Mmm. Why `to uppercase`? Wouldn't `std::uppercase` be a whole lot simpler? `sstr << std::showbase << std::uppercase << std::hex << 42 << "\n";` -> `0x2A` – sehe Oct 19 '13 at 20:08
  • @sehe You're right, I completely forgot about that manipulator! Thanks! – Sergey Kalinichenko Oct 19 '13 at 20:16
  • @sehe: Thanks for that. Is there a way to keep the base lowercase? – navig8tr Oct 19 '13 at 21:50
  • Maybe I'll just use this `return "0x" + hexString` – navig8tr Oct 19 '13 at 21:55
  • @navig8tr I'd do that in this case, in general: `std::nouppercase` is your friend – sehe Oct 19 '13 at 22:19
  • @navig8tr `return "0x" + hexString` is a good idea - it's nice and easy to read. – Sergey Kalinichenko Oct 19 '13 at 23:59
  • @NathanFellman It doesn't. That wasn't the OP's question, though. – Sergey Kalinichenko Dec 16 '13 at 13:17
  • 3
    Does anyone know a solution that would work with a bitset too big for a ulong? Also, OP, would you accept an answer to finish this Q&A please :) – Benjamin Maurer Feb 27 '14 at 14:29
  • @BenjaminMaurer current implementation (e.g. VS2013) has `.to_ullong()` function to convert to unsigned long long. Is that enough for you? – Micka May 26 '16 at 20:01
  • 1
    @Micka, thx, was added with C++11 (http://en.cppreference.com/w/cpp/utility/bitset/to_ullong). Can barely remember what I was using it for and it's too late for me, but good for future generations ;) – Benjamin Maurer May 27 '16 at 09:31
  • 1
    What if I wanted to do this for bitsets too long for even a ullong? Say, for example, for up to 256 bits? I defined my own struct for an int that holds 256 bits, but there is no std::bitset function to convert to it. Does anyone know where I can find the code for std::bitset.ullong() so I can try to tweak it for this case? – adam tropp Jan 23 '19 at 21:31
2

Here is an alternative for C:

unsigned int bintohex(char *digits){
  unsigned int res=0;
  while(*digits)
    res = (res<<1)|(*digits++ -'0');
  return res;
}

//...

unsigned int myint=bintohex("11001111");
//store value as an int

printf("%X\n",bintohex("11001111"));
//prints hex formatted output to stdout
//just use sprintf or snprintf similarly to store the hex string
technosaurus
  • 7,676
  • 1
  • 30
  • 52
1

Here is the easy alternative for C++:

bitset <32> data; /*Perform operation on data*/ cout << "data = " << hex << data.to_ulong() << endl;

dumb_pawn
  • 11
  • 1