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
9 7444 DeMan 1,806
Top Contributor
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.
RedSon 5,000
Recognized Expert Expert
I would agree with the masking but I'm not sure what you are hoping to accomplish in terms of portability.
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. -
struct{
-
unsigned month : 4;
-
unsigned day : 5;
-
unsigned hour : 5;
-
unsigned minute : 6;
-
unsigned diff : 1;
-
unsigned dhour : 5;
-
unsigned dmin : 6;
-
} fTimestamp;
-
Is the Structure I created in the headder file. -
int memread(int len, FILE *in){
-
unsigned char *buf; /* buffer */
-
size_t result; /* Number of bytes read */
-
int i;
-
unsigned int val;
-
-
if (len != 0){
-
/* Allocate memory to store bytes read*/
-
buf = ( unsigned char*) malloc (len);
-
if (buf == NULL) {fputs ("Memory error",stderr); exit (2);}
-
-
/* Read bytes into the buffer. Result holds number of bytes read */
-
result = fread(buf,1,len,in);
-
-
/* Display the value of each bit in hex and return 4 byte int value */
-
for (i=0;i<result;i++){
-
printf("The value of byte %d = %02X \n",i+1, buf[i]);
-
if (result == 1) {
-
val = 1677721600;
-
val = (val * 256);
-
val += buf[i];
-
} else if (result == 2){
-
if (i == 0){
-
val = 3429105664;
-
}
-
val = (val * 256);
-
val += buf[i];
-
} else {
-
val = (val * 256);
-
val += buf[i];
-
}
-
}
-
-
/* Cleanup */
-
free(buf);
-
} else {
-
val = 0;
-
}
-
-
return val;
-
}
-
Is the function I wotre to read the file in chucks based on specs for the file I was given. -
file_length = memread(fLength,fp);
-
printf("\nFile Length: %d \n", file_length);
-
header_length = memread(hLength,fp);
-
printf("Header Length: %d \n", header_length);
-
high_version_id = memread(hVersion,fp);
-
printf("High Version: %d \n", high_version_id);
-
low_version_id = memread(lVersion,fp);
-
printf("Low Version: %d \n", low_version_id);
-
file_open_timestamp = memread(fOpenTime,fp);
-
printf("File Open Timestamp: %u \n", file_open_timestamp);
-
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.mont h 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
DeMan 1,806
Top Contributor
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......
DeMan 1,806
Top Contributor
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......
Ok so with the data from above.
The int value of the timestamp is 2645522880 or 100111011010111 101111001110000 00 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?
DeMan 1,806
Top Contributor
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
WORKS LIKE A CHARM..... Thank you
Here is what I ended up with and it works perfectly. -
file_open_timestamp = memread(fOpenTime,fp);
-
printf("File Open Timestamp: %u \n", file_open_timestamp);
-
-
fTimestamp.month = ((file_open_timestamp & 0xf0000000) >> 28);
-
fTimestamp.day = ((file_open_timestamp & 0x0f800000) >> 23);
-
fTimestamp.hour = ((file_open_timestamp & 0x007C0000) >> 18);
-
fTimestamp.minute = ((file_open_timestamp & 0x0003f000) >> 12);
-
fTimestamp.diff = ((file_open_timestamp & 0x00000800) >> 11);
-
fTimestamp.dhour = ((file_open_timestamp & 0x000007C0) >> 6);
-
fTimestamp.dmin = (file_open_timestamp & 0x0000003f);
-
-
printf(" Month: %d \n",fTimestamp.month);
-
printf(" Day: %d \n",fTimestamp.day);
-
printf(" Hour: %d \n",fTimestamp.hour);
-
printf(" Minute: %d \n",fTimestamp.minute);
-
if (fTimestamp.diff == 1){
-
printf(" Diff: + \n");
-
} else {
-
printf(" Diff: - \n");
-
}
-
printf(" dHour: %d \n",fTimestamp.dhour);
-
printf(" dMin: %d \n",fTimestamp.dmin);
-
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
DeMan 1,806
Top Contributor Sign in to post your reply or Sign up for a free account.
Similar topics |
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".
|
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 download and
decode single part binaries.
I thought I'd post the code and see what people think. I'd appreciate
any feedback. It's my first program with generators and I'm worried
I'm making this twice and hard as it needs to be.
|
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 alternate servers for
missing pieces. This is the working code so far. It will walk a
newsgroup and download and decode all the single part attachments.
Just change server,user,password,group at the bottom to something less
|
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 another time zone.
It would be of great value if the time of some central source could be
drawn, in the form of a ".js" file, into the script to avoid
dependence on the client's date/time.
|
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 Scaleable Vector Graphics (SVG), which is a 2D XML application, <rect>s.
The problem is that it takes about 10 seconds to display the frame. I need
to approach real-time display if possible. So I thought a binary representation
of the data would...
| |
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
|
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
|
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. 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.
|
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 -
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx
and save to a file say test2.xml
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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,...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |