gpt4 book ai didi

c - C 中的按位移位

转载 作者:行者123 更新时间:2023-12-02 14:05:21 25 4
gpt4 key购买 nike

我最近决定开展一个短信项目,通过手机发送和接收短信。

数据以 PDU 格式发送 - 我需要将 ASCII 字符更改为 7 位 GSM 字母字符。为此,我遇到了几个示例,例如 http://www.dreamfabric.com/sms/hello.html

此示例显示了第二个七字节的最右边的位,被插入到第一个七字节中,以创建一个八字节。

按位移位不会导致这种情况发生,因为>>将插入到左侧,而<<将插入到右侧。据我了解,我需要某种按位旋转来创建这个 - 谁能告诉我如何从右侧移动位并将它们插入左侧?

谢谢

最佳答案

这里有一个快速算法来做到这一点:

int b1, bnext;
int mod;
int pand;
char *b; // this is your byte buffer, with message content
int blen; // this is your byte buffer length
char sb[160];
int totchars = 0;

b1 = bnext = 0;
for (int i=0; i < blen; i++) {
mod = i%7;
pand = 0xff >> (mod + 1);
b1 = ((b[i] & pand) << mod) | bnext;
bnext = (0xff & b[i]) >> (7 - mod);
sb[totchars++] = (char)b1;
if (mod == 6) {
sb[totchar++] = (char)bnext;
bnext = 0;
}
}
sb[totchar] = 0;

它将 7 位压缩缓冲区转换为 C 语言中的常规 ASCII 字符数组。

关于c - C 中的按位移位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2617470/

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