gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:37:37 28 4
gpt4 key购买 nike

我正在尝试将 C 中的 unsigned char 转换为 matlab 代码,unsigned char 的 vector 填充有十六进制值。 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'));

关于将 C 中的无符号字符转换为 MatLab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44251486/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com