Forum

Encode Bits into Bytes Hex and decode

hello

i was trying to find a way to encode 3 values of a range of 0-15 0-15 0-15 into 2 bytes maximum data.

something like encoding bits into bytes or hex characters .

as a byte is got 00000000 and a range of 0-15 can be express in bynary like
0000-1111 i want to put to of this pieces into a byte and and the other piece into other so to have maximum of 2 bytes for expressing 0-15 0-15 0-15 then decode it. I can only send a maximum of 2 bytes to some hardware but need to send this 3 values so i,m trying to find how to do this bitmasking trick.

i saw some nice plugs related https://github.com/jens-a-e/V4BitwiseOperators/blob/master/BitOperators.csproj

could you give me some hints ideas for it ?

BitsIntoByteHex.zip (19.0 kB)

Do you know if you can use all bits of your two bytes? Depending on the protocol of your hardware this may or may not be possible. Are you in control of decoding the bytes on the hardware? If no then the hardware must dictate how you need to encode the values.

AND, OR and bitshift should be all you need.

there are actually 3 ways you can combine and split the values. Radix (Value) should be the fastest… depending on the capabilities of your hardware you have to choose one of the methods there. probably bitwise operations…

BitsIntoByteHex2.v4p (61.3 kB)

hi

thanks guys, i was testing radix yesterday but did not get the way also i was using always ascii as it is the smaller way i found as i thought it is like a raw byte, so cool i was missing this AsRaw.

i was doing now in other more humble way ;D,doing little converter plugs so in the end sending in 2 bytes in ascci.

yes i,ll test later on and see how easy it is to decode, i will use arduino to decode it actually.

BitsIntoByteHexTry.rar (27.4 kB)

hi tonfilm , what about if i want to also use the second part of the second byte , what do i need to change ? , i tried just adding another pin to the radix but did not work .
Edit : ok i needed to change the Output Base i add another 0.

hello again

what about if i want to pack ranges of 0-31[0-150-70-70-7

i was doing like in this patch. but not sure about the minimum i can pack it and the output type.

by the way the other (0-7) ranges worked perfect thank you , i did in arduino something like this to read it :

//uint8_t lsb_R = CleanBufferA_loop & 0b00001111;
//uint8_t msb_G = (CleanBufferA_loop & 0b11110000) >> 4;
//uint8_t lsb_B = CleanBufferB_loop & 0b00001111;
//uint8_t msb_A = (CleanBufferB_loop & 0b11110000) >> 4;

cu ;D

CalculateBitBytes.rar (28.9 kB)

you can also do this with the bit operations in a general way:

Ranges_0_31_0_16_0_7_0_7_0_7_2.v4p (60.9 kB)

great ¡¡ thank you tonfilm