Connecting Tech Pros Worldwide Help | Site Map

how encode or decode date in binary

Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#1: Aug 19 '08
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. 31/09/2009 in binary in the file written as 4f 75 25 00
b. 31/05/2008 in binary in the file written as 5a 74 25 00

My problem is how the date would be written as above in binary. Also, how encode or decode.

Please help me.

Thanks in advance.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#2: Aug 19 '08

re: how encode or decode date in binary


What was the format of the data when it was written?

Was it:

int month
int slash
int day
int slash
int year

??

Or was it:
int month
char slash
int day
char slash
int year

Or was it:

char month
char month
char slash
char day
char day
char slash
char year
char year
char year
char year

??

You can't read file when you don't know the format of the data.
Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#3: Aug 19 '08

re: how encode or decode date in binary


the date format DD/MM/YYYY meaning

DD -date
slash (/) - just a separator
MM - month
slash (/) - just a separator
YYYY - year

so, data to be taken for decode or encode is DDMMYYYY only

Thanks
Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#4: Aug 19 '08

re: how encode or decode date in binary


Quote:

Originally Posted by saykenari

the date format DD/MM/YYYY meaning

DD -date
slash (/) - just a separator
MM - month
slash (/) - just a separator
YYYY - year

so, data to be taken for decode or encode is DDMMYYYY only

Thanks

all data must be in INTEGER example given 31052009
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#5: Aug 19 '08

re: how encode or decode date in binary


[quote=saykeenari]
all data must be in INTEGER example given 31052009
[/code]

If you can read 31052009 in the disc file, then you date is a string of char integers.

Further, it means the file is not in binary. If it was, you couldn't read the data directly with your eye.

From what you say, the disc file is in text mode and you need to read the date into a char array. Then you can parse the array and add the slashes.
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#6: Aug 20 '08

re: how encode or decode date in binary


Quote:

Originally Posted by saykenari

all data must be in INTEGER example given 31052009

So, must it be integer (32-bit) 30million-something for day 30 or the same 32 bits in unknown format you gave in first post (4f 75 25 00) ?
Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#7: Aug 20 '08

re: how encode or decode date in binary


Quote:

Originally Posted by newb16

So, must it be integer (32-bit) 30million-something for day 30 or the same 32 bits in unknown format you gave in first post (4f 75 25 00) ?

Yes, this in unsigned integer and using 32 bits to save the data. So, I am not sure how or technique used to encode or decode or using bitwise operator as mentioned in my first post. So,it is the main problem.

I hope you can understand it

Thank you
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#8: Aug 20 '08

re: how encode or decode date in binary


Quote:

Originally Posted by saykenari

Yes, this in unsigned integer and using 32 bits to save the data. So, I am not sure how or technique used to encode or decode or using bitwise operator as mentioned in my first post. So,it is the main problem.

I hope you can understand it

Thank you

I understand, but the data provided ( 2 samples ) is not enough to guess how data is encoded, are there any fields where day month and year are stored independently or is it flat ( days since some date ) number.
It seems however that it is low-endian 'days since ***' format ( as first minus second equals 245, that is number of days in year minus 120 days in four months )
So you need to convert it to 32bit integer, find where is zero point of the scale and go.
Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#9: Aug 21 '08

re: how encode or decode date in binary


Quote:

Originally Posted by newb16

I understand, but the data provided ( 2 samples ) is not enough to guess how data is encoded, are there any fields where day month and year are stored independently or is it flat ( days since some date ) number.
It seems however that it is low-endian 'days since ***' format ( as first minus second equals 245, that is number of days in year minus 120 days in four months )
So you need to convert it to 32bit integer, find where is zero point of the scale and go.

Oh , I am forgot that the first date as I mentioned is wrongly typed. The other sample data which same format DDMMYYYY I found as following :

Date : 31012009 written as 4f 75 25 00
Date : 30092009 written as 41 76 25 00
Date : 01122009 written as 7f 76 25 00
Date : 02012010 written as 9f 76 25 00
Date : 30012009 written as 4e 75 25 00
Date : 01012010 written as 9e 76 25 00


So, I am not understand how it got the above. Would you show the calculation to me and to be written in C?

Thank you.
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#10: Aug 21 '08

re: how encode or decode date in binary


Quote:

Originally Posted by saykenari

Oh , I am forgot that the first date as I mentioned is wrongly typed. The other sample data which same format DDMMYYYY I found as following :

Date : 31012009 written as 4f 75 25 00
Date : 30092009 written as 41 76 25 00
Date : 01122009 written as 7f 76 25 00
Date : 02012010 written as 9f 76 25 00
Date : 30012009 written as 4e 75 25 00
Date : 01012010 written as 9e 76 25 00


So, I am not understand how it got the above. Would you show the calculation to me and to be written in C?

Thank you.

Do you know how integer numbers are represented in computer?
Open windows calculatorm switch to hex. Enter 25769e( the last number, higher byte is the rightmost), press minus, enter 25754e, press '=', switch to decimal. You get 336, it is exactly a year minus one month, as the second is Dec 1 and the first is Dec 30. So the whole number looks like number of days since some date. If it were bit fields, it probably would have same bits equal if same year/month is equal, that is not this case ( 75-76 transition occurs within the same year ).
To convert it to day, monh and year, you need to find point zero, count leap years correctly (or at least as correct as the program created this format do ) find year, and convert remainder to month and day. I'm not going to do it because *) I have no original program to verify, *)it probably takes more than an hour and *)It is in no way challenging, rewarding or increasing my skills - rather boring and useless, let alone violating Teh Rulz.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#11: Aug 21 '08

re: how encode or decode date in binary


By that argument you do realise that sets the epoch back in 47 centurary BC?
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#12: Aug 22 '08

re: how encode or decode date in binary


Quote:

Originally Posted by Banfa

By that argument you do realise that sets the epoch back in 47 centurary BC?

If none of the higher bits actually represent some flags, there is no other guesses - at least it is correct on dates from 2008 to 2010 and no other dates are provided for us.... "When I save yellow square it is 0x12345, when I save green circle it is 0x54321, what should I do to save it as red triangle and convert it to binary? plz send codez in cpp"
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#13: Aug 22 '08

re: how encode or decode date in binary


Quote:

Originally Posted by newb16

If none of the higher bits actually represent some flags, there is no other guesses

Actually that is a good point, the 25 00 could be completely unrelated to the date in which case the figures give an epoch sometime in the middle of 1926, hmmmm still not conclusive though.

Definitely a case of more data required.
Newbie
 
Join Date: Aug 2008
Location: kuala lumpur, malaysia
Posts: 6
#14: Aug 23 '08

re: how encode or decode date in binary


Quote:

Originally Posted by Banfa

Actually that is a good point, the 25 00 could be completely unrelated to the date in which case the figures give an epoch sometime in the middle of 1926, hmmmm still not conclusive though.

Definitely a case of more data required.

I am not sure why end of byte is 25 00. I am still in dark to get the correct logic to get the right answer. The previous clue given, still not correct.
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#15: Aug 23 '08

re: how encode or decode date in binary


Quote:

Originally Posted by saykenari

I am not sure why end of byte is 25 00. I am still in dark to get the correct logic to get the right answer. The previous clue given, still not correct.

It is not 'end of byte', it is higher 16 bits of 32 bit integer. If you state 'still not correct' please provide data that ocontradict this suggestion so that we ( may ) rectify it.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#16: Aug 23 '08

re: how encode or decode date in binary


Quote:

Originally Posted by newb16

It is not 'end of byte', it is higher 16 bits of 32 bit integer.

My point is that you don't necessarily know that.

You could be looking at data that represents 2 16 bit integers, a 16 bit integer and 2 bytes, a 16 bit integer and the first 2 bytes of a 32 bit integer.

Since the OP does not appear to know the actual format of the binary data, does not appear to have given us a whole record and has not even told us what the record represents any suggestion of what the data types may be is speculation at best.

That the 4 bytes make up a 32 bit integer of number of days since a given data is possible but the given data is not enough to show it. We probably need some other examples from different years. 1 example of the data for a year in the 1800s would probably be quite edifying.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 828
#17: Aug 27 '08

re: how encode or decode date in binary


It would also be helpful to see two examples that differ by exactly one day.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#18: Aug 27 '08

re: how encode or decode date in binary


Quote:

Originally Posted by donbock

It would also be helpful to see two examples that differ by exactly one day.

Actually that already exists in the supplied data

31012009 and 30012009

They aren't next to each other because the data is not ordered.
Reply