Bryan Adams
Bryan Adams

Reputation: 3

Decode FOUR_BITS of a byte in a byte array (in C)

I am writing a code to decode the byte array based on user input (EIGHT_BITS at a time or FOUR_BITS at a time). I've actually managed to decode the byte array based on EIGHT_BITS. Now I want to decode them in terms of FOUR_BITS.

INT DecodeElem(UINT8 *decodeBuf, UINT8 elemlen, UINT8 *tempBuf, UINT8 elemlength){

  if (elemlength== EIGHT_BITS){
    *tempBuf = getByte(decodeBuf + decodeByteCount);
     decodeOffset = 0;
     decodeByteCount++;
  }
}

ie, if the elemlength= FOUR_BITS, I need to decode the first four bits of a particular byte in the byte array. Could someone let me know how do I do the same without modifying the case for the EIGHT_BITS which I have written above?

What I basically need is another if statement with if (elemlength == FOUR_BITS)

Note: tempBuf is CHAR * type and I can't change the type. decodeByteCount and decodeOffset are global variables; *decodeBuf is the already encoded byte array which needs to be decoded. elemlen is for future use and I will take care of it.

This is my getByte function:

UINT8 getByte(UINT8 *byteBuf)
{
  return ((UINT8)*byteBuf);
}

Upvotes: 0

Views: 272

Answers (1)

idoo
idoo

Reputation: 330

I'm not sure what decodeOffset and decodeByteCount does....
It should be something like that (assuming each byte has 2 4 bit values. If the assumption is wrong remove "/2" from the code):

if (elemlength== FOUR_BITS){  
    *tempBuf = getByte(decodeBuf + decodeByteCount / 2);
    if(decodeByteCount % 2)
        *tempBuff = (*tempBuff & 0xF0) >> 4;
    else
        *tempBuff = (*tempBuff & 0Xf);
    // ???decodeOffset = 0;
    // ???decodeByteCount++;
}

Upvotes: 1

Related Questions