working with binary files

This is a discussion on working with binary files within the C Programming forums, part of the General Programming Boards category; solved that to... sorry for my lack of experience... now i want to concatenate two bites from a byte to ...

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    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 ?

    thank you for your help!

  2. #17
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    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. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    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. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    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. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Binary Files Problem
    By terran9 in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2004, 09:23 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  4. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:12 PM
  5. Opening files in binary mode
    By ammar in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21