我想将C中的无符号字符转换为matlab代码,无符号字符的向量用十六进制值填充。下面的C代码:将C中的无符号字符转换为MatLab
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);
打印的值在每个代码不同,有人知道发生了什么事?
注意,你的C代码依赖于实现定义的行为;一个明确的方式来做我想你打算的将是'unsigned int temp =(value> SCHAR_MAX?0x1b:0);' –
这不是问题,C代码是德州仪器的库,用于MSP430系列MCU。我需要在MatLab中实现C代码来比较性能,因为我无法更改C代码。 –