We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi.
I´m reading serial data. Package is 81 bytes long. I´m saving it to and array and it works as such.
Problem is that the stream consists data of 16-bit integers and 8-bit chars mixed. So the data gets mixed in the array.
Data stream:
byte 0 = (int) first 8-bits information 1 -> goes to array[0]
byte 1 = second 8-bits information 1 -> goes to array[0]
byte 2 = (char) 8-bits information 2 -> goes to array[1]
byte 3 = (int) first 8-bits information 3 -> half goes to array [1] and rest array[2]
byte 4 = second 8-bits information 3 -> half goes to array [2] and rest array[3]
byte 5 = (char) 8-bits information 4 -> goes to array [3]
So when I read array[1] it would have combined values of two information.
I can´t do nothing about the send protocol.
So what is the best way to get the information so it don´t get mixed?
Answers
Bit shifting!
Example
Array[0]=8 bits
Last 4 bits
Char chr0last = array[0] & 0x0f
;First 4 bits
So now array[0]= (chr0first <<4) + chr0last;
This illustrates the concept of extracting group of bits. I used hex as it is customary. Last line I show how to merge group of bits to get new values (in this case I am restoring the. Orginal valuel)
I hope this helps,
Kf
Thanks kfrajer. Bit shifting works! Did a simpler version to make sense of it.
Thanks GoToLoop but your code is bit out of my talent range yet. But I will get back to it later.