Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert an array of bits to a char
    primarykey
    data
    text
    <p>I am trying to edit each byte of a buffer by modifying the LSB(Least Significant Bit) according to some requirements. I am using the unsigned char type for the bytes, so please let me know <strong>IF</strong> that is correct/wrong.</p> <pre><code>unsigned char buffer[MAXBUFFER]; </code></pre> <p>Next, i'm using this function </p> <pre><code>char *uchartob(char s[9], unsigned char u) </code></pre> <p>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.</p> <p>Here's where the hassle begins. I am going to point out what I'm trying to do <strong>step by step</strong> so you guys can let me know where i'm taking the wrong turn.</p> <p>I am <strong>saving the result</strong> of the above function (called for each element of the buffer) in a variable</p> <pre><code>char binary_byte[9]; // array of bits </code></pre> <p>I am <strong>testing the LSB</strong> simply comparing it to some flag like above. </p> <pre><code>if (binary_byte[7]==bit_flag) // i go on and modify it like this binary_byte[7]=0; // or 1, depending on the case </code></pre> <p>Next, I'm trying to <strong>convert the array of bits</strong> binary_byte (it is an array of bits, isn't it?) back into a byte/unsigned char <strong>and update the data in the buffer at the same time</strong>. I hope I am making myself clear enough, as I am really confused at the moment.</p> <pre><code>buffer[position_in_buffer]=binary_byte[0]&lt;&lt;7| // actualize the current BYTE in the buffer binary_byte[1]&lt;&lt;6| binary_byte[2]&lt;&lt;5| binary_byte[3]&lt;&lt;4| binary_byte[4]&lt;&lt;3| binary_byte[5]&lt;&lt;2| binary_byte[6]&lt;&lt;1| binary_byte[7]; </code></pre> <p>Keep in mind that the bit at the position binary_byte[7] may be modified, that's the point of all this.</p> <p>The solution is not really elegant, but <strong>it's working</strong>, even though i am really insecure of what i did (I tried to do it with bitwise operators but without success) </p> <p>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. <img src="https://i.stack.imgur.com/44bQ2.png" alt="enter image description here"></p> <p>My final question is : <strong>What effect does changing only the LSB in a byte have? What should I expect?</strong>. As you can see, I'm getting only "new" characters even when i shouldn't. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload