0

I'm currently working on a DBF file manager and I'm having some problems...

One of the elements that compose the header is the last date that the file was updated. The problem is: the field format is YYMMDD and it MUST have 3 bytes.

How is it possible to write a date using that format using only 3 bytes? Also, another field represents the type of the file (if it have a memo or not).

The file type in my case is 03h and this field MUST use only 1 byte. I'm pretty confused.

5
  • Well, think about how many different numbers can be represented with a single byte: 256. So, you can store any 2 (decimal) digit number (0-99) inside a single byte, with plenty of room to spare. Does this help? Commented Feb 8, 2014 at 20:07
  • I think i got the idea but how do I do it? I was using putc without sucess Commented Feb 8, 2014 at 20:10
  • What went wrong with putc? Commented Feb 8, 2014 at 20:15
  • I was not sure how to do it so I used putc to write the date, char by char. Is that wrong? Commented Feb 8, 2014 at 20:18
  • Chars take one byte each. So if you need to write 3 bytes exactly, you need to write a number which has the composition that you want. See my answer below. I hope I'm not misunderstanding your need. Commented Feb 8, 2014 at 20:20

2 Answers 2

1

I would hold your data in 3 bytes as

  • first byte = year
  • second byte = month
  • third byte = day

There's plenty of room in each byte for the domain of each field (year, month, day). You could write them in an integer with bit-shifting operations, like this:

int year = 13; int month = 7; int day = 26; int my_date = (year << 16) | (month << 8) | day; 

Edit:

What I did in my_date: I basically concatenated the information you need (year, month, day) into a series of bits (8 bits per information field), as an integer. You know an int is 4 bytes. Consider for starters that my_date is 0, that is, all 32 bits are 0. The 4 bytes of it are as follows ("|" denotes concatenation; that's for ease of reading):

my_date = 0 | 0 | 0 | 0 

When I write year << 16 I have

year << 16 = 0 | year | 0 | 0 

In a similar fashion,

month << 8 = 0 | 0 | month | 0 day = 0 | 0 | 0 | day 

When I apply the OR operator on all of them, my_date looks like this:

my_date = 0 | year | month | day 

Accessing them:

year = (my_date & 0xFF0000) >> 16; month = (my_date & 0xFF00) >> 8; day = my_date & 0xFF; 

Edit: how accessing works. We previously had

my_date = 0 | year | month | day 

If you do, for example, an AND with 0xFF00, which is 0 | 0 | FF | 0, you get

my_date & 0xFF00 = 0 | 0 | month | 0 

Now all you need to do is shift your data back, so

(my_date & 0xFF00) >> 8 = 0 | 0 | 0 | month = month 

Hope it's clearer now.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain what did you do in my_date? I'm still learning the language
0

First byte for year: 2000 + YY. Can count from 2000 till 2255

Second byte for month: 1-12

Third byte for day: 1-31

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.