big endian-small endian problem

This is a discussion on big endian-small endian problem within the C Programming forums, part of the General Programming Boards category; I m getting 37376 instead of 146 for width and 27393 instead of 363 for height. I guess big endian-small ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Unhappy big endian-small endian problem

    I m getting 37376 instead of 146 for width and 27393 instead of 363 for height. I guess big endian-small endian problem has occured. plz help me to solve this problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    }__attribute__((packed)) HEADER;
    
    int main(int argc,char **argv)
    {
            FILE *fptr;
           HEADER header;
    
            if (argc < 2) {
                    fprintf(stderr,"Usage: %s filename\n",argv[0]);
                    exit(-1);
            }
            if ((fptr = fopen(argv[1],"r+b")) == NULL) {
                    fprintf(stderr,"Unable to open GIF file \"%s\"\n",argv[1]);
                    exit(-1);
            }
            fread(&header,sizeof(HEADER),1,fptr);
           printf("\n Width = %d , Height =%d\n", header.width,header.height);
    Last edited by kapil1089thekin; 05-15-2008 at 03:24 PM. Reason: little mistake

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Haha, well idk what to tell you, it worked for me. The only thing I changed was a part of your struct because VS for some reason won't let me pack it that way.
    Code:
    #pragma pack(push, 1)
    typedef struct {
            char type[3];
            char code[3];
            unsigned short int width;
            unsigned short int height;
            char flags;
            char bgColorI;
            char pARatio;
    }  HEADER;
    #pragma pack(pop)
    If you believe this is an endian problem, do you know what your system is. This code worked for me and I am little endian.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    134
    I guess mine is also little endian, I m using Intel processor, I gues it wont work for big endian
    is there any function which can solve this problem??(NUXI problem)
    Last edited by kapil1089thekin; 05-15-2008 at 03:45 PM. Reason: slight error

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well you have several options.
    • You can make a function to reverse the order:
      Code:
      unsigned short reverse_short(unsigned short num)
      {
      	return (num << 8) | (num >> 8);
      }
    • You can call one of the networking functions like ntohs() to do the same.
    • You can read the file into a char buffer, and individually store each member of the struct.

    The advantage of the last two options is that your code will work on any platform.
    Last edited by swoopy; 05-15-2008 at 07:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem in C, Please Help.
    By DoctorFu in forum C Programming
    Replies: 6
    Last Post: 10-31-2005, 03:06 PM
  2. Small program big problem :-(
    By Fibre Optic in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2005, 09:53 AM
  3. Big HDD problem
    By biosninja in forum Tech Board
    Replies: 4
    Last Post: 08-18-2005, 07:08 AM
  4. Really big problem
    By arjunajay in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2005, 06:03 AM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 06:14 PM

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