6 Replies Latest reply on Feb 11, 2011 5:57 PM by Murray9654

# how to accumulate two or more bits and then covert into character?

Hi,
I am sending a character which is of 16 bit through a Socket. at the other end i am reading byte by byte. so at the other end after reading 2 bytes i want to combine them and convert into a char. So how do i do this? How do i combine two bytes and then convert into a char?
• ###### 1. Re: how to accumulate two or more bits and then covert into character?
bit shifting.

Take care with the endianness of the data or you might reverse the bytes.
• ###### 2. Re: how to accumulate two or more bits and then covert into character?
gimbal2 wrote:
bit shifting.
I don't want to shift bits. I just want to concatenate two bytes and then convert that into char. For this how is bit shifting helpful.

Suppose i have received 2 bytes "11111111" and "00000000" and now i want to join this two like "1111111100000000" and then convert into char.
• ###### 3. Re: how to accumulate two or more bits and then covert into character?
Muralidhar wrote:
gimbal2 wrote:
bit shifting.
I don't want to shift bits.
Yes you do.
I just want to concatenate two bytes and then convert that into char. For this how is bit shifting helpful.
Read the tutorial to find out.

>
Suppose i have received 2 bytes "11111111" and "00000000" and now i want to join this two like "1111111100000000" and then convert into char.
Done using shift operations.
• ###### 4. Re: how to accumulate two or more bits and then covert into character?
>

>
Read the tutorial to find out.
Suppose i have received 2 bytes "11111111" and "00000000" and now i want to join this two like "1111111100000000" and then convert into char.
Done using shift operations.
i have read the tutorial. see if the two bytes are "10101010" and "10101010" and if i want to concatenate how can the shifting helps. I am not able to figure out how this can be done? can somebody please explain? If not in detail can you give brief explanation?
• ###### 5. Re: how to accumulate two or more bits and then covert into character?
Muralidhar wrote:
>
Read the tutorial to find out.
Suppose i have received 2 bytes "11111111" and "00000000" and now i want to join this two like "1111111100000000" and then convert into char.
Done using shift operations.
i have read the tutorial. see if the two bytes are "10101010" and "10101010" and if i want to concatenate how can the shifting helps. I am not able to figure out how this can be done? can somebody please explain? If not in detail can you give brief explanation?
You put the most significant byte in a short or a char, shift it left, then put the least significant byte in it with a bitwise OR. Voilà, your "concatenated" bytes.
• ###### 6. Re: how to accumulate two or more bits and then covert into character?
>

>
You put the most significant byte in a short or a char, shift it left, then put the least significant byte in it with a bitwise OR. Voilà, your "concatenated" bytes.
great Thanks.