473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binary Date Time decode

All,

I was wondering if someone could point me in the right direction to decode the following information.
I am working on a tool writen in C and one of the items in the file I am decoding is a timestamp in this format.
I had started looking at a bitfield structure, but I am wondering if there is a better / more portable solution.

Hex Value in the file: 9D AF 79 C0
Binary breakdown:
1001(9) 1101(D) 1010(A) 1111(F) 0111(7) 1001(9) 1100(C) 0000(0)

1001(9) 11011(27) 01011(11) 110111(55) 1(+) 00111(7) 000000(0)
Decoded Value: 09/27 11:55 +07:00

Format:

These parameters indicate the time when the file was opened, according to the following binary format:
  • The first four binary bits indicate the month (1 .. 12), according to the CGF"s local time zone;
  • The next five binary bits contain the date (1 :: 31), according to the CGF"s local time zone;
  • The next five binary bits contain the hour (0 .. 23), according to the CGF"s local time zone;
  • The next six binary bits contain the minute (0 .. 59), according to the CGF"s local time zone;
  • The next bit indicates the sign of the local time differential from UTC (bit set to '1' expresses '+' or bit set to
  • The next five binary bits contain the hour (0 .. 23) deviation of the local time towards UTC, according to
  • The next six binary bits contain the minute (0 .. 59) deviation of the local time towards UTC, according to
Feb 26 '07 #1
9 7407
DeMan
1,806 1GB
Have you gotten anywhere with it yourself so far? Please post your progress.

This looks like it might need to use some bitshifts (which means fun for all the family), or at least some masking.
Feb 26 '07 #2
RedSon
5,000 Expert 4TB
I would agree with the masking but I'm not sure what you are hoping to accomplish in terms of portability.
Feb 26 '07 #3
Wow you guys are fast ^_^.

As for portability I was just concerned, after reading about bitfiled structurs that I may run into problems using this tool on other machines. All the systems that would use it should be x86 intel based systems so maybe it is not an issue.

As for where I have gotten so far.
Expand|Select|Wrap|Line Numbers
  1. struct{
  2.     unsigned month : 4;
  3.     unsigned day : 5;
  4.     unsigned hour : 5;
  5.     unsigned minute : 6;
  6.     unsigned diff : 1;
  7.     unsigned dhour : 5;
  8.     unsigned dmin : 6;
  9. } fTimestamp;
  10.  
Is the Structure I created in the headder file.

Expand|Select|Wrap|Line Numbers
  1. int memread(int len, FILE *in){
  2.     unsigned char *buf; /* buffer */
  3.     size_t result; /* Number of bytes read */
  4.     int i;
  5.     unsigned int val;
  6.  
  7.     if (len != 0){
  8.         /* Allocate memory to store bytes read*/
  9.         buf = ( unsigned char*) malloc (len);
  10.         if (buf == NULL) {fputs ("Memory error",stderr); exit (2);}
  11.  
  12.         /* Read bytes into the buffer. Result holds number of bytes read */
  13.         result = fread(buf,1,len,in);
  14.  
  15.         /* Display the value of each bit in hex and return 4 byte int value */
  16.         for (i=0;i<result;i++){
  17.             printf("The value of byte %d = %02X \n",i+1, buf[i]);
  18.             if (result == 1) {
  19.                 val = 1677721600;
  20.                 val = (val * 256);
  21.                 val += buf[i];
  22.             } else if (result == 2){
  23.                 if (i == 0){
  24.                     val = 3429105664;
  25.                 }
  26.                 val = (val * 256);
  27.                 val += buf[i];
  28.             } else {
  29.                 val = (val * 256);
  30.                 val += buf[i];
  31.             }
  32.         }
  33.  
  34.         /* Cleanup */
  35.         free(buf);
  36.     } else {
  37.         val = 0;
  38.     }
  39.  
  40.     return val;
  41. }
  42.  
Is the function I wotre to read the file in chucks based on specs for the file I was given.

Expand|Select|Wrap|Line Numbers
  1.     file_length = memread(fLength,fp);
  2.     printf("\nFile Length: %d \n", file_length);
  3.     header_length = memread(hLength,fp);
  4.     printf("Header Length: %d \n", header_length);
  5.     high_version_id = memread(hVersion,fp);
  6.     printf("High Version: %d \n", high_version_id);
  7.     low_version_id = memread(lVersion,fp);
  8.     printf("Low Version: %d \n", low_version_id);
  9.     file_open_timestamp = memread(fOpenTime,fp);
  10.     printf("File Open Timestamp: %u \n", file_open_timestamp);
  11.  
This is a few of the calling statemnts to the memread function.
The length of each item is defined as a constatnt in the header file.
Currently I am just printing the returned value to the screen.
In the end I will be writing all the values out to a MySQL load file.

The test I have done so far with the bitfiled values have showed me that whatever I pass to it it returs the binary value of the last n binary bits.
So for fTimestamp.month if I assign it 22 (10110) fTimestamp returns a value of 6 (0110). Showing me that whatever I pass it assigns the last 4 bits.

The reason for that test is that Im a bit stupmed how to break up the data to pass to the structure. Like fTimestamp.day is 5 bits binary, which from my 1st post would be the 4 bits from D (1101) and the 1st bit from A (1010) for a result of 27 (11011).

So I guess the big roadblock for me is how to read the stream in a way to produce the correct values.


Thanks for everyones input and suggestions
Feb 26 '07 #4
DeMan
1,806 1GB
You can mask certain values away....
If you have a bit string of length 16, say,: 1100 1101 0101 1100
and you AND (&) it with 0000 0000 1111 0000, say, you get rid of the values you have 0 in your mask, and keep those that are one. You could do this for 5 bits int your time stamp, so that you isolate a particular 5.

Once you have (and because we know the position int the bitstring), you can right (?) shift the value back as far as it needs. If you store it in a character variable (8 bits) you can easily get the value you require......
Feb 26 '07 #5
DeMan
1,806 1GB
I probably should have added, that none of this requires you to explicitly work at the bit level......

0000 0000 1111 0000 is an int with value 240(i think), and likewise any mask that you make, can be expressed as an integer......
Feb 26 '07 #6
Ok so with the data from above.
The int value of the timestamp is 2645522880 or 10011101101011110111100111000000 in binary.

So to get the first part of the timestamp ( the month) I would replace the last 28 bits with 0's, blanking out eveything but the 1st 4 and the shift it right to make those the last 4 bits?

Also for the second element ( the day) I would mask out the 1st 4 and the last 23 with 0's, then shift right to amke those the last 5 bits?
Feb 26 '07 #7
DeMan
1,806 1GB
Spot on. (take care with the bitshift for the leftmost value.....)


(As a total Aside, FYI) You could actually do the shifting before the masking. Since you often need to use the same sizes eg 5, you may be able to create standardised masks to extract the 5 bits from 8. I would discourage you from taking this approach, though, as it involves casting big numbers into small numbers. whcih can have unexpected behaviour
Feb 26 '07 #8
WORKS LIKE A CHARM..... Thank you
Here is what I ended up with and it works perfectly.

Expand|Select|Wrap|Line Numbers
  1.     file_open_timestamp = memread(fOpenTime,fp);
  2.     printf("File Open Timestamp: %u \n", file_open_timestamp);
  3.  
  4.     fTimestamp.month = ((file_open_timestamp & 0xf0000000) >> 28);
  5.     fTimestamp.day = ((file_open_timestamp & 0x0f800000) >> 23);
  6.     fTimestamp.hour = ((file_open_timestamp & 0x007C0000) >> 18);
  7.     fTimestamp.minute = ((file_open_timestamp & 0x0003f000) >> 12);
  8.     fTimestamp.diff = ((file_open_timestamp & 0x00000800) >> 11);
  9.     fTimestamp.dhour = ((file_open_timestamp & 0x000007C0) >> 6);
  10.     fTimestamp.dmin = (file_open_timestamp & 0x0000003f);
  11.  
  12.     printf(" Month: %d \n",fTimestamp.month);
  13.     printf(" Day: %d \n",fTimestamp.day);
  14.     printf(" Hour: %d \n",fTimestamp.hour);
  15.     printf(" Minute: %d \n",fTimestamp.minute);
  16.     if (fTimestamp.diff == 1){
  17.         printf(" Diff: + \n");
  18.     } else {
  19.         printf(" Diff: - \n");
  20.     }
  21.     printf(" dHour: %d \n",fTimestamp.dhour);
  22.     printf(" dMin: %d \n",fTimestamp.dmin);
  23.  
Here is the ouput from the 1st post.
File Open Timestamp: 2645522880
Month: 9
Day: 27
Hour: 11
Minute: 55
Diff: +
dHour: 7
dMin: 0
Feb 26 '07 #9
DeMan
1,806 1GB
Beautiful....
Feb 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
1
by: fishboy | last post by:
Howdy, I'm in middle of a personal project. Eventually it will download multipart binary attachments and look for missing parts on other servers. And so far I've got it to walk a newsgroup and...
0
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search...
5
by: Gord | last post by:
Many scripts and calendars call client side system time in order to make presentations. However, the client's time may be improperly set, if set at all, and/or the relevant time may be from...
11
by: Andrew Neiderer | last post by:
I am sort of new to XML and SVG. So if this has been addressed before please be patient and/or just point me to a URL. I have written a Java application which generates lots (150 x 150) of...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
1
by: Andrey | last post by:
Hi I donno if this is the right place to ask for this question, anyway.... Is it possible to pack binary data into simplejson? d={} d = xxxxxx d = xxxxxx d = xxxxx
18
by: saykenari | last post by:
Hello Friends I have a problem date format written as DD/MM/YYYY. Date already save into a file as binary. I don't know which technique should be used. Sample date as following a....
0
by: Carl Forsman | last post by:
I have some binary data encoded as Base64 encoding in an XML element like this: <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx</data> how can I extract this data -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.