开发者

instantiating bitset using hex character

开发者 https://www.devze.com 2022-12-29 19:55 出处:网络
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

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.


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);
}


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];
}


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 );
      ...
    }

}


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


One more way..

void fn(char c)
{
    unsigned int hexValue = 0;
    char str[2] = {c, '\0'};
    sscanf(&str, "%X", &hexValue);
    bitset<4> temp(hexValue);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消