简体   繁体   English

如何将位数组转换为char

[英]How to convert an array of bits to a char

I am trying to edit each byte of a buffer by modifying the LSB(Least Significant Bit) according to some requirements. 我试图根据一些要求修改LSB(最低有效位)来编辑缓冲区的每个字节。 I am using the unsigned char type for the bytes, so please let me know IF that is correct/wrong. 我使用unsigned char类型作为字节,所以请告诉我, 如果这是正确/错误的。

unsigned char buffer[MAXBUFFER];

Next, i'm using this function 接下来,我正在使用此功能

char *uchartob(char s[9], unsigned char u)

which modifies and returns the first parameter as an array of bits. 它修改并返回第一个参数作为位数组。 This function works just fine, as the bits in the array represent the second parameter. 此函数工作正常,因为数组中的位代表第二个参数。

Here's where the hassle begins. 这就是麻烦开始的地方。 I am going to point out what I'm trying to do step by step so you guys can let me know where i'm taking the wrong turn. 我将逐步指出我正在尝试做什么所以你们可以让我知道我在哪里走错了路。

I am saving the result of the above function (called for each element of the buffer) in a variable 我在变量中保存上述函数的结果 (调用缓冲区的每个元素)

char binary_byte[9];             // array of bits

I am testing the LSB simply comparing it to some flag like above. 我正在测试LSB,只需将它与上面的一些标志进行比较。

if (binary_byte[7]==bit_flag)     // i go on and modify it like this
binary_byte[7]=0;                // or 1, depending on the case

Next, I'm trying to convert the array of bits binary_byte (it is an array of bits, isn't it?) back into a byte/unsigned char and update the data in the buffer at the same time . 接下来,我试图将比特数组 binary_byte(它是一个位数组,不是吗?)转换回字节/无符号字符并同时更新缓冲区中的数据 I hope I am making myself clear enough, as I am really confused at the moment. 我希望自己足够清楚,因为我此刻真的很困惑。

buffer[position_in_buffer]=binary_byte[0]<<7| // actualize the current BYTE in the buffer
                        binary_byte[1]<<6|
                        binary_byte[2]<<5|
                        binary_byte[3]<<4|
                        binary_byte[4]<<3|
                        binary_byte[5]<<2|
                        binary_byte[6]<<1|
                        binary_byte[7];

Keep in mind that the bit at the position binary_byte[7] may be modified, that's the point of all this. 请记住,binary_byte [7]位置的位可能会被修改,这就是所有这一点。

The solution is not really elegant, but it's working , even though i am really insecure of what i did (I tried to do it with bitwise operators but without success) 解决方案并不是很优雅,但是它正在工作 ,即使我对我所做的事情感到非常不安全(我尝试使用按位运算符但没有成功)

The weird thing is when I am trying to print the updated character from the buffer. 奇怪的是当我尝试从缓冲区打印更新的字符时。 It has the same bits as the previous character, but it's a completely different one. 它与前一个字符具有相同的位,但它是完全不同的。 在此输入图像描述

My final question is : What effect does changing only the LSB in a byte have? 我的最后一个问题是: 只改变一个字节中的LSB有什么影响? What should I expect? 我应该期待什么? . As you can see, I'm getting only "new" characters even when i shouldn't. 正如你所看到的,即使我不应该,我也只会获得“新”角色。

bitwise: 按位:

set bit => a |= 1 << x; set bit => a |= 1 << x;

reset bit => a &= ~(1 << x); reset bit => a &= ~(1 << x);

bit check => a & (1 << x); bit check => a & (1 << x);

flip bit => a ^= (1 << x) 翻转位=> a ^= (1 << x)

If you can not manage this you can always use std::bitset . 如果你无法管理它,你总是可以使用std :: bitset

Helper macros : 助手宏

#define SET_BIT(where, bit_number) ((where) |= 1 << (bit_number))

#define RESET_BIT(where, bit_number) ((where) &= ~(1 << (bit_number)))

#define FLIP_BIT(where, bit_number) ((where) ^= 1 << (bit_number))

#define GET_BIT_VALUE(where, bit_number) (((where) & (1 << (bit_number))) >> bit_number) //this will retun 0 or 1 #define GET_BIT_VALUE(where, bit_number) (((where) & (1 << (bit_number))) >> bit_number) //这将重新调整0或1

Helper application to print bits : 帮助应用程序打印位

#include <iostream>
#include <cstdint>

#define GET_BIT_VALUE(where, bit_number) (((where) & (1 << (bit_number))) >> bit_number)

template<typename T>
void print_bits(T const& value)
{
    for(uint8_t bit_count = 0;
        bit_count < (sizeof(T)<<3);
        ++bit_count)
    {
        std::cout << GET_BIT_VALUE(value, bit_count) << std::endl;
    }
}

int main()
{
    unsigned int f = 8;
    print_bits(f);
}

So I'm still a little unsure what you are trying to accomplish here but since you are trying to modify individual bits of a byte I would propose using the following data structure: 所以我仍然不确定你要在这里完成什么,但是因为你试图修改一个字节的各个位,我建议使用以下数据结构:

union bit_byte
{
    struct{
        unsigned bit0 : 1;
        unsigned bit1 : 1;
        unsigned bit2 : 1;
        unsigned bit3 : 1;
        unsigned bit4 : 1;
        unsigned bit5 : 1;
        unsigned bit6 : 1;
        unsigned bit7 : 1;
    } bits;

    unsigned char all;
};

This will allow you to access each bit of your byte and still get your byte representation. 这将允许您访问字节的每个位,仍然可以获得字节表示。 Here some quick sample code: 这里有一些快速示例代码:

bit_byte myValue;
myValue.bits.bit0 = 1;    // Set the LSB

// Test the LSB
if(myValue.bits.bit0 == 1) {
    myValue.bits.bit7 = 1;
}

printf("%i", myValue.all);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM