473,750 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading date stored in binary fomrat

Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:
Thanks.
Samuel.

//necessary headers assumed to have been included
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
_tzset();
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTim e = time(&_theTempD ate);
char* theStringTime = ctime(&_theTemp Time);
struct tm* _theTM = gmtime(&_theTem pDateTime);
struct tm* _theTML = localtime(&_the TempDate);
return 0;
Nov 14 '05 #1
4 4775
Ekong, Samuel Akpan wrote:
Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value.
Off the top of my head, I don't think any of the standard date or time
functions read a date or time in a binary format, so it's not surprising
that you are having problems.
It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:
Write code to parse it. That's the best advice I can give. No standard
function is likely to do it for you, and I have no idea what the format
is, so I can't suggest any particular method of parsing it.
Thanks.
Samuel.

//necessary headers assumed to have been included
Unless you show them, we assume otherwise. Also, this style of comment
is fine if you are using C99, but C99 compilers are very rare at the
moment. This is a syntax error in other versions of C.
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )
_putenv is not a standard function, and if it's one of yours then it
violates the implementation' s namespace.
{
printf( "Unable to set TZ\n" );
Call of a variadic function possibly without a prototype in scope.
exit( 1 );
This is not a portable exit code. The standard gives meaning to exactly
three exit codes: 0, EXIT_SUCCESS, and EXIT_FAILURE. Also, exit() may
not have a prototype in scope.
}
else
{
_tzset();
Non-standard, violates implementation namespace.
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );
Variadic function used possibly without a prototype, and none of these
names used as arguments have been declared anywhere (and they may
violate the implementation' s namespace).
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTim e = time(&_theTempD ate);
char* theStringTime = ctime(&_theTemp Time);
struct tm* _theTM = gmtime(&_theTem pDateTime);
struct tm* _theTML = localtime(&_the TempDate);
Declarations may not follow executable code in C, unless you are using
C99. Also, you may be missing declarations for all these functions and
types. Finally, I strongly recommend never beginning an identifier with
an underscore. It appears to be OK in this particular case (because they
are local variables), but in general such identifiers may be reserved
for the implementation' s use.
return 0;


Missing '}'.

Generally, if you want to get help here, you should post code that is 1)
complete, 2) standard, and 3) as short as it can be while still
demonstrating the problem. This is so that we can can copy, paste, and
compile the code. You've failed to meet 1 and 2. Your description of the
problem also leaves something to be desired, since you didn't tell us
anything about the format of the date.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2
On Sun, 04 Jan 2004 18:48:35 GMT, "Ekong, Samuel Akpan"
<sa**********@s bcglobal.net> wrote in comp.lang.c:
Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(little-endian).
Encoded by whom, using what algorithm?
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in
the year 1970. I have been able to read the time accurately but I just want
to be able to parse correctly the date. Any assistance will be appreciated.
Skeleton sample code shown below:
Thanks.
Samuel.

//necessary headers assumed to have been included
int main(int argc, char* argv[])
{
if( _putenv( "TZ=PST-PDT" ) == -1 )
Whoops, no _putenv() in the C library...
{
printf( "Unable to set TZ\n" );
exit( 1 );
}
else
{
_tzset();
Whoops, no _tzset() function in the C library...
printf( "_daylight = %d\n", _daylight );
printf( "_timezone = %ld\n", _timezone );
printf( "_tzname[0] = %s\n", _tzname[0] );
printf( "_tzname[1] = %s\n", _tzname[1] );
}

time_t _theTempTime= 38965;
time_t _theTempDate= 731553;
time_t _theTempDateTim e = time(&_theTempD ate);
char* theStringTime = ctime(&_theTemp Time);
struct tm* _theTM = gmtime(&_theTem pDateTime);
struct tm* _theTML = localtime(&_the TempDate);
return 0;


There are two problems here, the first is that you haven't said, and
perhaps don't know, what algorithm/language/program was used to write
the binary value into the file. The second is that your code is
littered with calls to non-standard extensions that are specific to
your compiler/operating system combination.

You need to ask this question in a group that supports that particular
compiler/OS combination of yours.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
On Sun, 04 Jan 2004 13:48:35 -0500, Ekong, Samuel Akpan wrote:
Hi,
I am parsing a binary file and intend to read a date(not time) field
encoded thus:
0xA1290B(little-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the
known datetime functions to return this value either as a tm structure
or as a string value. It's more accurate to say that my attempts return
a date in the year 1970. I have been able to read the time accurately
but I just want to be able to parse correctly the date. Any assistance
will be appreciated. Skeleton sample code shown below: Thanks.


I've tried several combinations of endianess and with known formats but
I think you have to be missing a byte because 24bits of data is very
small for a data format. Otherwise it could be some screwy microsoft
format that breaks down fields into certain bits.

#include <stdlib.h>
#include <stdio.h>
#include <encdec.h>

int
main(int argc, char *argv[])
{
unsigned char data[4];
unsigned long v1 = 0xa1290b;
unsigned long v2 = 0x0b29a1;
time_t t;
enc_uint32le(v1 , data);

t = dec_time(data, TIME_1970_SEC_3 2LE);

printf("%s", ctime(&t));

return EXIT_SUCCESS;
}

Encdec is available here:

http://www.ioplex.com/~miallen/encdec/

Mike
Nov 14 '05 #4
On Sun, 04 Jan 2004 18:48:35 GMT, "Ekong, Samuel Akpan"
<sa**********@s bcglobal.net> wrote:
Hi,
I am parsing a binary file and intend to read a date(not time) field encoded
thus:
0xA1290B(littl e-endian).
Now I know the date to be 12/04/2003 but just cannot get any of the known
datetime functions to return this value either as a tm structure or as a
string value. It's more accurate to say that my attempts return a date in <snip>
time_t _theTempTime= 38965;
time_t _theTempDate= 731553;

<snip>

Projecting the current calendar system back to January 1, year 1,
ignoring the calendar reforms that took place, there would have been
719162 days from 1-1-1 thru 12-31-1969, the day before unix day zero.
If you subtract 719162 from 731553, you get 12391. Multiply that by
86400 seconds per day, feed the result into gmtime, and you get Dec 5
2003. I don't know if the one-day difference between that and your
expected result is due to the time zone or the need to subtract one
more day.

Bill

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3776
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 databases, arrays, etc. but have not worked with binary data files. http://www.performancecentral.net/C...3Loop/3Loop.php click on "Download Performance".
3
9620
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID, next 4 byte (int) = serial number etc. The first problem is Big Endian/ Little Endian problem. I can decipher if the format is big or little endian. But got confuse as to how to decipher the data.
8
9530
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 contains the following three floating-point numbers: 1.0 2.0 3.0
3
432
by: KevinD | last post by:
thank you for your helpful explanations. In my first note I forgot to mention that my simple flatfile is a text file with a newline character at the end thus I able to get an entire record . Dave V. - I understand your description but as described above, I get all 5 data fields in one file read using fgets.
1
2019
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
30
4578
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
21
6390
by: EdUarDo | last post by:
Hi all, I'm not a newbie with C, but I don't use it since more than 5 years... I'm trying to read a text file which has doubles in it: 1.0 1.1 1.2 1.3 1.4 2.0 2.1 2.2 2.3 2.4 I'm doing this (it's only a test trying to achieve the goal...):
11
3594
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
2
4099
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date: 10212009 12 34 56 25 67 90 End Set ******** Set: 2 Date: 10222009
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4713
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.