c语言中字符数组的位操作

c语言中字符数组的位操作,c,bit-manipulation,C,Bit Manipulation,如果给我一个大小为8的字符数组,我知道前3个字节是id,下一个字节是消息,最后3个字节是值。如何使用位操作来提取消息 示例:字符数组包含999011(每个位置一个整数),其中999是id,0是消息,111是值 有什么建议吗?谢谢 通常的方法是定义一个结构,其成员是位字段,与数组中的分段信息相对应。(哦,重新阅读您的问题:数组是否填充了{'9','9',…}?,然后您只需将具有适当偏移量的值放入数组中。通常的方法是定义一个结构,其成员是位字段,与数组中的分段信息相对应。(哦,重新阅读您的问题:数组

如果给我一个大小为8的字符数组,我知道前3个字节是id,下一个字节是消息,最后3个字节是值。如何使用位操作来提取消息

示例:字符数组包含999011(每个位置一个整数),其中999是id,0是消息,111是值


有什么建议吗?谢谢

通常的方法是定义一个结构,其成员是位字段,与数组中的分段信息相对应。(哦,重新阅读您的问题:数组是否填充了
{'9','9',…}
?,然后您只需将具有适当偏移量的值放入数组中。

通常的方法是定义一个结构,其成员是位字段,与数组中的分段信息相对应。(哦,重新阅读您的问题:数组是否填充了
{'9','9',…}
?,然后您只需将具有适当偏移量的值放入数组中即可。

给定:

数组包含{'9'、'9'、'9'、'0'、'1'、'1'、'1'}

然后,您可以使用
sscanf()
进行转换:

但是这里不需要位操作。

给定:

数组包含{'9'、'9'、'9'、'0'、'1'、'1'、'1'}

然后,您可以使用
sscanf()
进行转换:


但是这里不需要位操作。

好吧,如果你想进行位操作,不管怎样,这里是:

#include <stdio.h>
#include <arpa/inet.h>

int main(void) {
        char arr[8] = "9997111";
        int msg = 0;

        msg = ((ntohl(*(uint32_t *) arr)) & 0xff) - 48;

        printf("%d\n", msg);

        return 0;
}
请记住一件事…这不符合严格的别名规则。但是您可以使用一些memcpy()的东西来解决它

编辑#1(将其全部解析,授予严格的别名规则遵从性,并让您看到这没有任何意义):


好吧,如果你想进行位操作,不管怎样,它是这样的:

#include <stdio.h>
#include <arpa/inet.h>

int main(void) {
        char arr[8] = "9997111";
        int msg = 0;

        msg = ((ntohl(*(uint32_t *) arr)) & 0xff) - 48;

        printf("%d\n", msg);

        return 0;
}
请记住一件事…这不符合严格的别名规则。但是您可以使用一些memcpy()的东西来解决它

编辑#1(将其全部解析,授予严格的别名规则遵从性,并让您看到这没有任何意义):


您可以使用内存拷贝来提取值

    char *info = malloc(sizeof(int)*3);
    char *info2 = malloc(sizeof(int)*1);
    char *info3 = malloc(sizeof(int)*3);

    memcpy(info,msgTest, 3);
    memcpy(info2,msgTest+3, 1);
    memcpy(info3,msgTest+4, 3);
    printf("%s\n", msgTest);
    printf("ID is %s\n", info);
    printf("Code is %s\n", info2);
    printf("Val is %s\n", info3);
假设字符串msgTest=“0098457

print语句如下所示

身份证号码是009 代码是8 瓦尔是457


希望这有帮助,祝你好运!

你可以使用内存拷贝来提取值。下面是一个例子

    char *info = malloc(sizeof(int)*3);
    char *info2 = malloc(sizeof(int)*1);
    char *info3 = malloc(sizeof(int)*3);

    memcpy(info,msgTest, 3);
    memcpy(info2,msgTest+3, 1);
    memcpy(info3,msgTest+4, 3);
    printf("%s\n", msgTest);
    printf("ID is %s\n", info);
    printf("Code is %s\n", info2);
    printf("Val is %s\n", info3);
假设字符串msgTest=“0098457

print语句如下所示

身份证号码是009 代码是8 瓦尔是457


希望这会有所帮助,祝你好运!

这里有一个例子,在堆栈有限的嵌入式设备上,我没有使用malloc或内存拷贝来实现一个好的实现。注意,没有必要使用compact,因为它只有1个字节。这是C11实现。如果你有4个字节要分析,请创建另一个st将地址复制到新结构,这与嵌入式设计模式的概念一致

#include <stdio.h>

// start by creating a struct for the bits
typedef struct  {
    unsigned int bit0:1; //this is LSB
    unsigned int bit1:1; //bit 1
    unsigned int bit2:1;
    unsigned int bit3:1;
    unsigned int bit4:1;
    unsigned int bit5:1;
    unsigned int bit6:1;
    unsigned int bit7:1;
    unsigned int bit8:1;
}charbits;


int main()
{
    // now assume we have a char to be converted into its bits
    char a = 'a'; //asci of a is 97
    charbits *x;   //this is the character bits to be converted to
    // first convert the char a to void pointer
    void* p; //this is a void pointer
    p=&a;    // put the address of a into p
    //now convert the void pointer to the struct pointer
    x=(charbits *) p;

    // now print the contents of the struct
    printf("b0 %d b1 %d b2 %d b3 %d b4 %d b5 %d b6 %d b7 %d", x->bit0,x->bit1, x->bit2,x->bit3, x->bit4, x->bit5, x->bit6, x->bit7, x->bit8);

    // 97 has bits like this 01100001
    //b0 1 b1 0 b2 0 b3 0 b4 0 b5 1 b6 1 b7 0 
    // now we see that bit 0 is the LSB which is the first one in the struct

    return 0;
}
    // thank you and i hope this helps
#包括
//首先为位创建一个结构
类型定义结构{
无符号整数位0:1;//这是LSB
无符号整数位1:1;//位1
无符号整数比特2:1;
无符号整数比特3:1;
无符号整数位4:1;
无符号整数位5:1;
无符号整数位6:1;
无符号整数位7:1;
无符号整数比特8:1;
}charbits;
int main()
{
//现在假设我们有一个字符要转换成它的位
字符a='a';//a的asci为97
charbits*x;//这是要转换为的字符位
//首先将char转换为void指针
void*p;//这是一个void指针
p=&a;//将a的地址放入p
//现在将void指针转换为struct指针
x=(字符*)p;
//现在打印结构的内容
printf(“b0%db1%db2%db3%db4%db5%db6%db7%d”,x->bit0,x->bit1,x->bit2,x->bit3,x->bit4,x->bit5,x->bit6,x->bit7,x->bit8);
//97有类似于0110001的位
//b0 1 b1 0 b2 0 b3 0 b4 0 b5 1 b6 1 b7 0
//现在我们看到位0是LSB,它是结构中的第一个
返回0;
}
//谢谢你,我希望这能有所帮助

这里有一个例子,在堆栈有限的嵌入式设备上,我不使用malloc或memory copy来实现一个好的实现。注意,没有必要使用compact,因为它只有1个字节。这是C11实现。如果有4个字节要分析,请创建另一个包含4个字符位的结构,然后使用c将地址复制到新结构。这与嵌入式系统的设计模式概念一致

#include <stdio.h>

// start by creating a struct for the bits
typedef struct  {
    unsigned int bit0:1; //this is LSB
    unsigned int bit1:1; //bit 1
    unsigned int bit2:1;
    unsigned int bit3:1;
    unsigned int bit4:1;
    unsigned int bit5:1;
    unsigned int bit6:1;
    unsigned int bit7:1;
    unsigned int bit8:1;
}charbits;


int main()
{
    // now assume we have a char to be converted into its bits
    char a = 'a'; //asci of a is 97
    charbits *x;   //this is the character bits to be converted to
    // first convert the char a to void pointer
    void* p; //this is a void pointer
    p=&a;    // put the address of a into p
    //now convert the void pointer to the struct pointer
    x=(charbits *) p;

    // now print the contents of the struct
    printf("b0 %d b1 %d b2 %d b3 %d b4 %d b5 %d b6 %d b7 %d", x->bit0,x->bit1, x->bit2,x->bit3, x->bit4, x->bit5, x->bit6, x->bit7, x->bit8);

    // 97 has bits like this 01100001
    //b0 1 b1 0 b2 0 b3 0 b4 0 b5 1 b6 1 b7 0 
    // now we see that bit 0 is the LSB which is the first one in the struct

    return 0;
}
    // thank you and i hope this helps
#包括
//首先为位创建一个结构
类型定义结构{
无符号整数位0:1;//这是LSB
无符号整数位1:1;//位1
无符号整数比特2:1;
无符号整数比特3:1;
无符号整数位4:1;
无符号整数位5:1;
无符号整数位6:1;
无符号整数位7:1;
无符号整数比特8:1;
}charbits;
int main()
{
//现在假设我们有一个字符要转换成它的位
字符a='a';//a的asci为97
charbits*x;//这是要转换为的字符位
//首先将char转换为void指针
void*p;//这是一个void指针
p=&a;//将a的地址放入p
//现在将void指针转换为struct指针
x=(字符*)p;
//现在打印结构的内容
printf(“b0%db1%db2%db3%db4%db5%db6%db7%d”,x->bit0,x->bit1,x->bit2,x->bit3,x->bit4,x->bit5,x->bit6,x->bit7,x->bit8);
//97有类似于0110001的位
//b0 1 b1 0 b2 0 b3 0 b4 0 b5 1 b6 1 b7 0
//现在我们看到位0是LSB,它是结构中的第一个
返回0;
}
//谢谢你,我希望这能有所帮助

为什么需要位操作才能从char[8]数组中提取此类数据?出于某种原因是否需要此操作?是的。请使用&运算符按位执行AND@threadp不,我想我可以提取那个特定的值,但我想我可以尝试使用位操作。
#include <stdio.h>

// start by creating a struct for the bits
typedef struct  {
    unsigned int bit0:1; //this is LSB
    unsigned int bit1:1; //bit 1
    unsigned int bit2:1;
    unsigned int bit3:1;
    unsigned int bit4:1;
    unsigned int bit5:1;
    unsigned int bit6:1;
    unsigned int bit7:1;
    unsigned int bit8:1;
}charbits;


int main()
{
    // now assume we have a char to be converted into its bits
    char a = 'a'; //asci of a is 97
    charbits *x;   //this is the character bits to be converted to
    // first convert the char a to void pointer
    void* p; //this is a void pointer
    p=&a;    // put the address of a into p
    //now convert the void pointer to the struct pointer
    x=(charbits *) p;

    // now print the contents of the struct
    printf("b0 %d b1 %d b2 %d b3 %d b4 %d b5 %d b6 %d b7 %d", x->bit0,x->bit1, x->bit2,x->bit3, x->bit4, x->bit5, x->bit6, x->bit7, x->bit8);

    // 97 has bits like this 01100001
    //b0 1 b1 0 b2 0 b3 0 b4 0 b5 1 b6 1 b7 0 
    // now we see that bit 0 is the LSB which is the first one in the struct

    return 0;
}
    // thank you and i hope this helps