Printing the data in the right format

This is a discussion on Printing the data in the right format within the C Programming forums, part of the General Programming Boards category; Hi Guys I am reading some array records from a device, i know the first 4 bytes i am reading ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    34

    Printing the data in the right format

    Hi Guys
    I am reading some array records from a device, i know the first 4 bytes i am reading in is the Calender date and time, how can i print this in a time format? Any help will be highly appreciated

    Regards

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you have to know how the data is being stored. Number of seconds since some arbitrary start time? Anyway, check out time.h.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by dwks View Post
    Hi There
    All i know is that the first 4 bytes contain date and time information, Is there a way that i use this information to convert it into date and time by using some of the predefined functions in the above mentioned website.

    Regards

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    There are too many ways that those four bytes could represent the date and time. Do you know the data format?

    If not, you'll just have to guess. Here's my first guess:
    Code:
    time_t t;
    FILE *fp;
    fread(&t, sizeof t, 1, fp);
    puts(ctime(&t));
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by dwks View Post
    There are too many ways that those four bytes could represent the date and time. Do you know the data format?

    If not, you'll just have to guess. Here's my first guess:
    Code:
    time_t t;
    FILE *fp;
    fread(&t, sizeof t, 1, fp);
    puts(ctime(&t));
    But this example will be readng from the file, i have my data saved in a data array, how would the above example be used to read the first four bytes from a array?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you have to know how the data is represented in the array. How did you read it in, and what kind of thing did you store it in?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (sizeof(time_t) != 4) printf("Huh? time_t is not 4 bytes\n");
    else {
       time_t t;
       t = *(time_t *)&array[0];
    }
    This makes the assumption that the array location is such that a time_t object can be read directly from it. This is definitely OK on an x86 machine, but an array of char can be place at any byte boundary, and some processors will not be able to read a multibyte word from an unaligned address.

    --
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by matsp View Post
    Code:
    if (sizeof(time_t) != 4) printf("Huh? time_t is not 4 bytes\n");
    else {
       time_t t;
       t = *(time_t *)&array[0];
    }
    This makes the assumption that the array location is such that a time_t object can be read directly from it. This is definitely OK on an x86 machine, but an array of char can be place at any byte boundary, and some processors will not be able to read a multibyte word from an unaligned address.

    --
    Mats

    This is what i am tryin to do:

    /*pnt2 is of type ubyte*/
    pnt2 = &ans.data[8];
    time_t t;
    for(i=0;i<ans.data[2];i++)
    {
    if(array_type == 0) /* Analog array, each element is 4 bytes*/
    {
    ans_f.B[0] = pnt2[0];
    ans_f.B[1] = pnt2[1];
    ans_f.B[2] = pnt2[2];
    ans_f.B[3] = pnt2[3];
    pnt2 += 4;
    t = ans_f.F;
    printf("%s\n",ctime(&t));

    This code is printing me the following thing

    Wed Dec 31 17:00:00 1969

    Does not seem right too me....i hope this helps you guys what i am trying to get.

  10. #10
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by c_geek View Post
    This code is printing me the following thing

    Wed Dec 31 17:00:00 1969

    Does not seem right too me....i hope this helps you guys what i am trying to get.
    Sorry, I am not following... What do you think the output should be?
    How it should the output look like?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Prefer you use code tags next time. If not, the board will strip all whitespace for your indentation and sometimes code is turned into smilies as well.
    Last edited by Elysia; 12-21-2007 at 03:14 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by c_geek View Post
    This is what i am tryin to do:

    /*pnt2 is of type ubyte*/
    pnt2 = &ans.data[8];
    time_t t;
    for(i=0;i<ans.data[2];i++)
    {
    if(array_type == 0) /* Analog array, each element is 4 bytes*/
    {
    ans_f.B[0] = pnt2[0];
    ans_f.B[1] = pnt2[1];
    ans_f.B[2] = pnt2[2];
    ans_f.B[3] = pnt2[3];
    pnt2 += 4;
    t = ans_f.F;
    printf("%s\n",ctime(&t));

    This code is printing me the following thing

    Wed Dec 31 17:00:00 1969

    Does not seem right too me....i hope this helps you guys what i am trying to get.
    I'm guessing that means that t==0 (since that's what would be printed for a UNIX timestamp of 0 (midnight 1/1/70 GMT) on the east coast of the USA (GMT -5)).

    Am I supposed to know what ans_f.F is and how it relates to ans_f.B? 'Cause I'm guessing that t is not set correctly.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    34
    Quote Originally Posted by tabstop View Post
    I'm guessing that means that t==0 (since that's what would be printed for a UNIX timestamp of 0 (midnight 1/1/70 GMT) on the east coast of the USA (GMT -5)).

    Am I supposed to know what ans_f.F is and how it relates to ans_f.B? 'Cause I'm guessing that t is not set correctly.
    Hi
    i have defined a union so that i can collect the 4 bytes of data out of my ans.data. Here is the union struct:

    Code:
    union
    {
      TLS_UBYTE B[4];		        	/* No. of Bytes in Answer */
      TLS_FLOAT F;	            		/* Floats in Answer */
    }ans_f

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by c_geek View Post
    Hi
    i have defined a union so that i can collect the 4 bytes of data out of my ans.data. Here is the union struct:

    Code:
    union
    {
      TLS_UBYTE B[4];		        	/* No. of Bytes in Answer */
      TLS_FLOAT F;	            		/* Floats in Answer */
    }ans_f
    It's possible that time_t is an integer, so that the assignment causes a cast and truncation of your data (interpreted as a float). Often floats are stored exponents first, so if your four-byte integer doesn't have any 1's in the first seven bits or so, you have a subnormal (read: very small) number.
    Last edited by tabstop; 12-20-2007 at 05:06 PM. Reason: I can't read

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's definitely wrong to make it a float and then an integer [time_t is definitely not a float value].

    --
    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.

Page 1 of 4 1234 LastLast
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, 10:52 AM
  3. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 11:47 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05: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

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