使用十六进制字符实例化位集

发布于 2024-09-01 04:32:54 字数 374 浏览 8 评论 0原文

嘿,我正在尝试弄清楚如何基于十六进制字符实例化 4 位位集。例如,如果我有一个值为“F”的字符,我想创建一个大小为 4 的位集并初始化为 1111,或者如果它是 A,我想将其初始化为 1010。我可以使用一堆 if 语句,如下所示:

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

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

  //...

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

  //...

}

这效率不高,有没有一种方法可以轻松地将字符串转换为十进制整数并使用 int 的最后 4 位构造位集?

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那一片橙海, 2024-09-08 04:32:54

该类型有一个构造函数,它接受一个unsigned long< /代码>。将您的字符转换为相应的整数值并用它构造一个bitset

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

您可以根据需要实现假设的 hexchar_to_int。您可以使用 strtoul 来实现。例如:

unsigned long hexchar_to_int(char c) {
  char chars[2] = {c, '\0'};
  return strtoul(chars, NULL, 16);
}

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);
}
情痴 2024-09-08 04:32:54

如前所述,您需要一个 long 作为构造函数。你怎么得到这个?设置查找数组:

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

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];
}
薄荷梦 2024-09-08 04:32:54

std::bitset 有一个采用 unsigned long 的构造函数。你可以使用它:

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

}

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

}
岁吢 2024-09-08 04:32:54

尝试使用此函数来转换十六进制字符(处理大写和小写)。然后,将结果传递给 bitset 构造函数。您必须先检查输入是否有效!

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

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; 
}
塔塔猫 2024-09-08 04:32:54

还有一种方法..

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

One more way..

void fn(char c)
{
    unsigned int hexValue = 0;
    char str[2] = {c, '\0'};
    sscanf(&str, "%X", &hexValue);
    bitset<4> temp(hexValue);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文