C 中的二进制掩码和位操作

Binary Masks and Bits manipulation in C

我有一个任务,但我不知道我应该做什么。这是任务:

编写如下函数: char * encodingToShortString (char * dig_str);

该函数应该创建并 return 一个新字符串 short_dig_strshort_dig_str 中的每个字节将由 dig_str 的两个连续字符的两个相应的四位字节组成。 对于长度为 n(长度,不是大小)的 dig_strshort_dig_str 的长度将是 n /偶数 n 为 2,奇数 n 为 n / 2 + 1。 对于奇数 n,short_dig_str 中的第一个四位不匹配 dig_str 中的任何数字,其所有位都是零。

示例: 对于 dig_str = "1234",字符串 short_dig_str 将由以下整数组成: 00010010 00110100

对于dig_str = "51234",字符串short_dig_str将由以下整数组成: 00000101 00010010 00110100

(从左到右,最重要的MSB,到最不重要的LSB)。

需要的内存space必须准确分配给short_dig_str字符串。 可以假设有足够的内存进行分配。

我是这样启动函数的:

char* codingToShortString(char* dig_str)//let's imagine that dig_str[] = "12";
{
   char *short_dig_str;
   char temp;//0000 0000
   int n = strlen(dig_str);
   unsigned mask = 1<<3;//1111
   unsigned c; //bit counter
   if (n%2 == 0)
   {
     short_dig_str = malloc(((n/2)+1)*sizeof(char));
   }
   else
   {
     short_dig_str = malloc(((n/2)+2)*sizeof(char));
   }
   for (i=0; i<n; i++)
   {
     for (c=1; c<=4; c++)
      {
        temp = dig_str[i] & mask;
        temp <<= 1;
      }
   }
}

但是后来我不知道该怎么办。我如何将二进制值放入 short_dig_str?我很困惑。

先看看想要的输出:

Example: For dig_str = "1234", the string short_dig_str will consist of the following integer: 00010010 00110100

For dig_str = "51234", the string short_dig_str will consist of the following integer: 00000101 00010010 00110100

“整数”是指(无符号)字符。如果您将结果写为十六进制值,您将得到

"1234" => 0x12, 0x34

"51234" => 0x05, 0x12, 0x34

您采用的方法过于复杂。 您不需要任何位掩码。

char* codingToShortString(char* dig_str)
{
  int n = strlen(dig_str);

  // Add 1 before dividing to "round up", add 1 for [=10=]
  char *short_dig_str = malloc((n+1)/2 + 1);

  unsigned char digits;
  int out_pos = 0;  // Read index within input: "12345"
  int in_pos = 0;   // Write index within output: {0x01,0x23,0x34}

  // First handle odd number of digits
  // Foe even numbers no special treatment needed.
  if (n%2 != 0)
  {
    digits = dig_str[in_pos++] - '0';
    short_dig_str[out_pos++] = digit;
  }

  // Then handle remaining digits (as pairs!).
  for ( ; in_pos < n; )
  {
      digits  = (dig_str[in_pos++] -'0') << 4; // one digits in upper half ...
      digits |= dig_str[in_pos++] - '0';       // ... one digit in lower half

      // Store into result array...
      short_dig_str[out_pos++] = digits;
  }

  return short_dig_str;
}

由于返回的指针不是用作字符串而是用作存储 2 个小数的原始字节,因此它应该是 unsigned charuint8_t 等而不是 char 但你的签名按原样定义。

名称 codingToShortString 具有误导性,因为没有创建字符串(也没有以 0 结尾)。

糟糕的名字,糟糕的类型...我会说这不是一个很好的任务...