# Thread: working with binary files

1. solved that to... sorry for my lack of experience...

now i want to concatenate two bites from a byte to another byte. i have to use a macro

smthing like this

a: 10101011
b: 11101001

i want to obtain 1011101001 (take the first two from the begining of a and put them in the front of b) can i do this using a macro ?

2. You could for example mask out the 6 unwanted bit from a by using an appropriate bitmask and then shift the result two bits to the left and use bitwise OR to "add" b to the result.

You could also shift a 6 bits to the right, then shift 8 bits to the left and use bitwise OR to "add" b to the result. This will maybe cause a warning by the compiler.

This can of course be done with a makro.

3. Code:
`#define C(x) (x>>7&1)*256+(x>>6&1)*128`
i've done smthing like this because i only needed the decimal number.

next problem... i have a 4 byte entry that is an unsigned int, the problem is that i used a char to read the binary file. how can i obtain the 4 byte int ?

4. Code:
```#include <stdio.h>
#include <limits.h>

static const char Bit[] = "01";

char *bits_uchar(char *dest, unsigned char value)
{
char *start = dest;
unsigned char bit;
for ( bit = UCHAR_MAX / 2 + 1; bit > 0; bit >>= 1 )
{
*dest++ = Bit[!!(value & bit)];
}
*dest = 0;
return start;
}

char *bits_ushort(char *dest, unsigned short value)
{
char *start = dest;
unsigned short bit;
for ( bit = USHRT_MAX / 2 + 1; bit > 0; bit >>= 1 )
{
*dest++ = Bit[!!(value & bit)];
}
*dest = 0;
return start;
}

int main(void)
{
unsigned char  a = 0xAB, b = 0xE9;
unsigned short c = ((a & 0xC0) << 2) | b;
char binary[sizeof c * CHAR_BIT + 1];
printf("a =   0x%02X =         %s\n", a, bits_uchar(binary, a));
printf("b =   0x%02X =         %s\n", b, bits_uchar(binary, b));
printf("c = 0x%04X = %s\n",           c, bits_ushort(binary, c));
return 0;
}

/* my output
a =   0xAB =         10101011
b =   0xE9 =         11101001
c = 0x02E9 = 0000001011101001
*/```
http://www.daniweb.com/code/snippet150.html

5. My endianness link was a little vague.
Code:
```#include <stdio.h>

int main(void)
{
unsigned char b[] = {0x12,0x34,0x56,0x78};
unsigned long value = (b[0] << 24) | (b[1] << 16)  | (b[2] << 8) | b[3];
printf("value = 0x%08lX\n", value);
return 0;
}

/* my output
value = 0x12345678
*/```
Code:
```#include <stdio.h>

int main(void)
{
unsigned char b[] = {0x12,0x34,0x56,0x78};
unsigned long value = (b[3] << 24) | (b[2] << 16)  | (b[1] << 8) | b[0];
printf("value = 0x%08lX\n", value);
return 0;
}

/* my output
value = 0x78563412
*/```
Results may vary depending on the endianness of the target.

6. how can i do this viceversa, i have the long and i want the char [4]
i've tryed smthing like this but it doesn't work well:
Code:
```int main()
{
unsigned char a[5]={00000};
unsigned long x,i;
printf("x=");scanf("%ld",&x);
a[0]=a[0]|(x>>8);
x=x>>8;
a[1]=a[1]|(x>>8);
x=x>>8;
a[2]=a[2]|(x>>8);
x=x>>8;
a[3]=a[3]|(x>>8);
for(i=0;i<4;i++) printf("%x ",a[i]);
printf("\n");
return 0;
}```

7. Perhaps like this.
Code:
```#include <stdio.h>
#include <limits.h>

void foo(unsigned long value)
{
unsigned char byte[sizeof value];
size_t i;
for ( i = 0; i < sizeof byte / sizeof *byte; ++i )
{
byte[i] = value >> (CHAR_BIT * i);
printf("byte[%lu] = 0x%02X\n", (long unsigned)i, byte[i]);
}
}

int main(void)
{
foo(0x12345678UL);
return 0;
}

/* my output
byte[0] = 0x78
byte[1] = 0x56
byte[2] = 0x34
byte[3] = 0x12
*/```