有谁知道一种有效的方法来将一些字符的ASCII值插入到16位数字的8个最低有效位(LSB)中?
我脑海中唯一的想法是将这两个数字转换成二进制,然后用8位的ASCII值替换最后8个字符,从16位数字开始。但据我所知,字符串操作在计算时间上是非常昂贵的。
谢谢
发布于 2013-05-03 13:31:59
下面是@user1118321 idea的MATLAB实现:
%# 16-bit integer number
x = uint16(30000);
%# character
c = 'a';
%# replace lower 8-bit
y = bitand(x,hex2dec('FF00'),class(x)) + cast(c-0,class(x))发布于 2013-05-03 12:52:06
我不知道Matlab的语法,但在C中,它可能是这样的:
short x; // a 16-bit integer in many implementations
... do whatever you need to to x ...
char a = 'a'; // some character
x = (x & 0xFF00) | (short)(a & 0x00FF);&运算符是算术"and“运算符。|运算符是算术"or“运算符。为了便于阅读,以0x开头的数字是十六进制的。
https://stackoverflow.com/questions/16351725
复制相似问题