将 C 中的无符号字符转换为 MatLab

Convert unsigned char in C to MatLab

我正在尝试将 C 中的 unsigned char 转换为 matlab 代码,unsigned char 的向量填充有十六进制值。 C 代码下方:

int main()
{
  unsigned char value = 0xaa;
  signed char temp;
  // cast to signed value
  temp = (signed char) value;
  // if MSB is 1, then this will signed extend and fill the temp variable with 1's
  temp = temp >> 7;
  // AND with the reduction variable
  temp = temp & 0x1b;
  // finally shift and reduce the value
  printf("%u",((value << 1)^temp));
}

我创建的用于执行相同操作的 Matlab 函数:

value = '0xaa';
temp = int8(value);
temp2 = int8(value);
temp = bitsra(temp,7);
temp = and(temp,'0x1b');
galois_value =   xor(bitsll(temp2,1),temp);
disp(galois_value);

每个代码打印的值不同,有人知道发生了什么吗?

您创建了一个字符串:

value = '0xaa';

共 4 个字符,['0' 'x' 'a' 'a']

在 MATLAB 中,您通常不会按位处理变量,但如果需要,请尝试:

temp = int8(hex2dec('aa'));