Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can we extract byte. For ex, if int takes 4 bytes and from that I have to extract 1st byte or 3 rd byte. How we can do that?
Posted
Updated 6-Jun-11 1:49am
v4
Comments
cuteband 6-Jun-11 3:54am    
show your example code in here so we can revise it.

An 'elegant' way uses union:

C
union Mixed 
{
  int c;
  unsigned char  b[4];
};

int main()
{
  int i;
  union Mixed m;
  m.c = 65535;
  printf("int: %d.\n");
  for (i=0; i<4; i++)
  {
    printf("byte[%d]: %d\n",i, m.b[i]);
  }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 15:05pm    
Much better, my 5. The approach with shift also good, but could be done more universal.
--SA
PrafullaVedante 7-Jun-11 3:54am    
Good one.
You need value at the bit position. means a binary value rt?

this is possible using bitwise shift operator.
suppose I is your integer and you need to get its Nth bit value.
this possible by following steps
a = right shift I by (N-1)
b = right shift I by N
c = Left shift b by 1.
R = a XOR c;
R is your Nth Bit value in binary. either 1 or 0
c function may look liek below
C#
int GetBit( int I, int N)
{
int a = I >> ( N - 1);
int b = I >> N;
b = b << 1;
int r = a ^ b;
return r;
}


and this will not work only for N = 32.
that logic you need to write seprate.
Hope this is what you are looking for
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-11 15:04pm    
My 4. It would be more universal if you use sizeof.
--SA
You can take a character pointer to the integer variable , and then using the pointer you can have the required byte

Function would look like this


char GetByteFrom( int i, int i_iByteIndex)
{

  char * cPointer = (char*)&i;
  return *(cPointer + i_iByteIndex);
  
}
 
Share this answer
 
Use memcpy()[^] function if you need single byte. Also you could consider using operator & if you just need something like a bit mask
 
Share this answer
 
Just add a macro to get specific byte / word.

C#
#define LOWORD(l)           ((WORD)((DWORD_PTR)(l) & 0xffff))
#define HIWORD(l)           ((WORD)((DWORD_PTR)(l) >> 16))
#define LOBYTE(w)           ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w)           ((BYTE)((DWORD_PTR)(w) >> 8))


The below function will give more optimistic view

C#
byte GetByte( unsigned int usValue_i, int nByteNumber_i )
{
    byte byVal = 0;
    unsigned short usVal = 0;
    if( 2 >= nByteNumber_i )
    {
        usVal = ( usValue_i & 0xffff );
    }
    else if( 4 >= nByteNumber_i )
    {
        usVal = ( usValue_i >> 16 );
    }
    if(( 3 == nByteNumber_i ) || ( 1 == nByteNumber_i ))
        byVal = ( usVal & 0xff );
    else if(( 4 == nByteNumber_i ) || ( 2 == nByteNumber_i ))
        byVal = ( usVal >> 8 );
    return byVal;
}
int main()
{
    unsigned int unVal = 0xFFEEDDCC;
    byte by1 = GetByte( unVal, 1 );
    byte by2 = GetByte( unVal, 2 );
    byte by3 = GetByte( unVal, 3 );
    byte by4 = GetByte( unVal, 4 );
    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900