tags:

views:

233

answers:

5

Hey, I'm trying to figure out how to instantiate a 4 bit bitset based on a hex character. For instance, If I have a character with value 'F', I want to create a bitset of size 4 initialized to 1111 or if it is A, i want to initialize it to 1010. I could use a bunch of if statements like so:

fn(char c)
{
  bitset<4> temp;

  if(c == 'F')
    temp.set();

  //...

  if(c == '9')
  {
    temp.set(1);
    temp.set(3);
  }

  //...

}

This isn't efficient, is there a way of easily converting the string to a decimal integer and constructing the bitset using the last 4 bits of the int?

Thanks for any help.

A: 

Try this function to convert a hex character (handles both uppercase and lowercase). Then, pass the result to the bitset constructor. You must check that the input is valid first!

int HexLetterToInt(char input) 
{ 
    if(input >= '0' && input <= '9') 
    { 
        input -= '0'; 
    } 
    else if(input >= 'A' && input <= 'F') 
    { 
        input -= 'A' - 10;
    } 
    else if(input >= 'a' && input <= 'f') 
    { 
        input -= 'a' - 10;
    } 
    else 
    { 
        // Not a valid character
        input = -1;
    } 
    return input; 
}
In silico
A: 

std::bitset has a constructor that takes unsigned long. You could use it:

void fn( char c ) {
    if ( c >= '0' && c <= '9' ) {
      bitset<4> temp( c - '0' );
      ...
    }
    if ( c >= 'A' && c <= 'F' ) {
      bitset<4> temp( c - 'A' + 0xA );
      ...
    }
    if ( c >= 'a' && c <= 'f' ) {
      bitset<4> temp( c - 'a' + 0xA );
      ...
    }

}
Kirill V. Lyadvinsky
'F' minus '0' is not 15.
Matthew Flaschen
yes, missed that at first.
Kirill V. Lyadvinsky
+2  A: 

That type has a constructor that accepts an unsigned long. Convert your character to the corresponding integral value and construct a bitset with that.

std::bitset<4> temp(hexchar_to_int(c));

You can implement the hypothetical hexchar_to_int however you want. You might use strtoul for that. For example:

unsigned long hexchar_to_int(char c) {
  char chars[2] = {c, '\0'};
  return strtoul(chars, NULL, 16);
}
Rob Kennedy
A common trick I've seen for `hexchar_to_int` is to declare a string `"0123456789ABCDEF"` and do an `indexOf`.
polygenelubricants
Clever. But then don't you just need another trick for `indexOf`?
Rob Kennedy
Not really, that's just a `std::find(begin, end, X)-begin`. Breaks horribly when someone calls it with `f` though.
MSalters
Of course. I guess my point was just that it doesn't really seem like much of a "trick" if its description refers to calling a function that doesn't even exist. (But calling a function that doesn't exist *would* be quite a trick!)
Rob Kennedy
A: 

One more way..

void fn(char c)
{
    unsigned int hexValue = 0;
    char str[2] = {c, '\0'};
    sscanf(&str, "%X", &hexValue);
    bitset<4> temp(hexValue);
}
Jujjuru
@Rob Thanks. Corrected the problem.
Jujjuru
A: 

As noted, you need a long for the constructor. How do you get that? Set up a lookup array:

const int BASE = MIN('0', MIN('a', 'A')); // std::min isn't an ICE
const int SIZE = MAX('9', MAX('f', 'F')) - BASE;
long lookup[SIZE];
lookup['0' - BASE] = 0; lookup['1' - BASE] = 1;
lookup['2' - BASE] = 2; lookup['3' - BASE] = 3;
lookup['4' - BASE] = 4; lookup['5' - BASE] = 5;
lookup['6' - BASE] = 6; lookup['7' - BASE] = 7;
lookup['8' - BASE] = 8; lookup['9' - BASE] = 9;
lookup['A' - BASE] = 10; lookup['a' - BASE] = 10;
lookup['B' - BASE] = 11; lookup['b' - BASE] = 11;
lookup['C' - BASE] = 12; lookup['c' - BASE] = 12;
lookup['D' - BASE] = 13; lookup['d' - BASE] = 13;
lookup['E' - BASE] = 14; lookup['e' - BASE] = 14;
lookup['F' - BASE] = 15; lookup['f' - BASE] = 15;
// ...
inline std::bitset<4> BitsFromChar(char c) {
  return lookup[c-BASE];
}
MSalters