Thread: Printing the data in the right format

  1. #46
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by matsp View Post
    Some slight modification to the format:
    Code:
    #include <stdio.h>
    
    int array[] = 
    {0x00000000,
     0x1fa42c33,
     0x1c202c33,
    
     0x189c2c33,
     0x15182c33,
     0x11942c33,
    
     0x0e102c33,
     0x0a8c2c33,
     0x07082c33,
    
     0x03842c33,
     0x00002c33,
     0x50dc2c32,
    };
    
    
    union timestamp {
       int tdate;
       struct {
          unsigned day:5;
          unsigned month:4;
          unsigned year: 4;
    	  unsigned :1;
          unsigned seconds: 17;
       } bits;
    };
    
    void swap(unsigned char &a, unsigned char &b)
    {
    	unsigned char t;
    	t = a;
    	a = b;
    	b = t;
    }
    
    void byteswap(int &t)
    {
    	unsigned char *x = (unsigned char *)&t;
        swap(x[0], x[3]);
    	swap(x[1], x[2]);
    }
    
    int main(void)
    {
    	int i;
    	timestamp tt;
    	for(i = 1; i < 12; i++)
    	{
    		tt.tdate = array[i];
    		printf("%04d-%02d-%02d %02d:%02d:%02d\n", tt.bits.year+2001, tt.bits.month, tt.bits.day, 
    				tt.bits.seconds / 3600, (tt.bits.seconds % 3600) / 60, tt.bits.seconds % 60);
    	}
    	return 0;
    }
    Gives this output:
    Code:
    2007-01-19 09:00:00
    2007-01-19 08:00:00
    2007-01-19 07:00:00
    2007-01-19 06:00:00
    2007-01-19 05:00:00
    2007-01-19 04:00:00
    2007-01-19 03:00:00
    2007-01-19 02:00:00
    2007-01-19 01:00:00
    2007-01-19 00:00:00
    2007-01-18 23:00:00
    Thanks Mat for the code, one last thing where are you exactly calling the "byteswap" function. I don't see a call to that function in either main or outside main. Thanks once again

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by c_geek View Post
    Thanks Mat for the code, one last thing where are you exactly calling the "byteswap" function. I don't see a call to that function in either main or outside main. Thanks once again
    No, because my first attempt was a bit "strange", so I tried swapping the bytes around, but it didn't make any more sense that way, so I shuffled it around in a different way, and that appears to work. I'm still a bit suspicious that the time counts down instead of up, but I guess that's just how it happens to be.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by matsp View Post
    No, because my first attempt was a bit "strange", so I tried swapping the bytes around, but it didn't make any more sense that way, so I shuffled it around in a different way, and that appears to work. I'm still a bit suspicious that the time counts down instead of up, but I guess that's just how it happens to be.

    --
    Mats
    So it means i dont need to have those two functions, i can just simply copy the first 4 bytes into the time_stamp union and i wil be alright?

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by c_geek View Post
    So it means i dont need to have those two functions, i can just simply copy the first 4 bytes into the time_stamp union and i wil be alright?
    Yes-

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Data Format Error Detection.
    By Ti22 in forum C Programming
    Replies: 2
    Last Post: 01-03-2004, 11:52 AM
  3. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 12:47 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM