简体   繁体   English

将C中的unsigned char转换为MatLab

[英]Convert unsigned char in C to MatLab

I am trying to convert unsigned char in C to matlab code, the vector of unsigned char is filled with hexadecimal values. 我试图将C中的unsigned char转换为matlab代码,unsigned char的向量用十六进制值填充。 Below the C code: 在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));
}

The Matlab function that I create to do the same thing: 我创建的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);

The values printed are different in each code, someone knows what is happening? 每个代码中打印的值都不同,有人知道发生了什么吗?

You have created a string: 您已创建一个字符串:

value = '0xaa';

Of 4 characters, ['0' 'x' 'a' 'a'] . 4个字符, ['0' 'x' 'a' 'a']

In MATLAB you don't generally handle variables bit wise, but if you want, try: 在MATLAB中,您通常不会逐位处理变量,但如果您愿意,请尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM