C 中的按位移位

发布于 2024-08-28 14:56:33 字数 383 浏览 12 评论 0原文

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

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

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

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

谢谢,

I've recently decided to undertake an SMS project for sending and receiving SMS though a mobile.

The data is sent in PDU format - I am required to change ASCII characters to 7 bit GSM alphabet characters. To do this I've come across several examples, such as http://www.dreamfabric.com/sms/hello.html

This example shows Rightmost bits of the second septet, being inserted into the first septect, to create an octect.

Bitwise shifts do not cause this to happen, as >> will insert to the left, and << to the right. As I understand it, I need some kind of bitwise rotate to create this - can anyone tell me how to move bits from the right handside and insert them on the left?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-09-04 14:56:33

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

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 字符数组。

Here is a quick algorithm to do that:

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;

It will convert 7bit compressed buffers to regular ASCII char arrays in C.

雨轻弹 2024-09-04 14:56:33

谁能告诉我如何移动位
从右侧插入
他们在左边吗?

C 中有一些间接的方法,但我只是这样做:

void main()
{
    int x = 0xBAADC0DE;
    __asm
    {
        mov eax, x;
        rol eax, 1;
    }
}

这会将位向左旋转(而不是移位!)(一步)。
“ror”将向右旋转。

can anyone tell me how to move bits
from the right handside and insert
them on the left?

There are indirect ways in C but I'd simply do it like this:

void main()
{
    int x = 0xBAADC0DE;
    __asm
    {
        mov eax, x;
        rol eax, 1;
    }
}

This will rotate (not shift!) the bits to the left (one step).
"ror" will rotate to the right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文