473,405 Members | 2,262 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,405 software developers and data experts.

Date to 4 byte string

Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

When I do something like this,

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

I get a hex # 15 characters long.

Any suggestions? I only have 4 bytes to work with to stay backward
compatible.

Thanks,
Steve
Nov 5 '07 #1
15 4316

"Steve" <ki****@harpservices.comwrote in message
news:1b********************************@4ax.com...
Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?
Let's see. 4 bytes will hold an unsigned 32-bit integer. Convert the date
into number of days past some specific date, and store it as that.
Nov 5 '07 #2
On 2007-11-05 09:57:51 -0800, Steve <ki****@harpservices.comsaid:
Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?
Not in a general purpose way. However, you could easily pick a
specific reference date, and then store the total number of days from
that date as the future date. 4 bytes gives you a 32-bit counter, and
you can represent a date VERY far in the future with that as your
number of days. :)

Actually 32-bits is (as you can see from the above) a pretty large
amount of data for the kind of information you're trying to store. So
depending on your desires and needs, you could waste some space making
the 4 bytes a little more readable and likely still come up with
something usable. For example, one byte each for day of month and
month of year, and then two bytes for the year. That would still allow
you to represent absolute dates (rather than the relative suggested
above) through the year 65535. 65536 if you're willing to take
advantage of the fact that there's no 0 AD.

This all assumes the usual Western Gregorian calendar. Your mileage
may vary depending on other calendars you might use, if any.

Pete

Nov 5 '07 #3
Hi,

I think you could do it, you could use one byte for the day, one for the
month and you have two for the year.
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Steve" <ki****@harpservices.comwrote in message
news:1b********************************@4ax.com...
Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

When I do something like this,

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

I get a hex # 15 characters long.

Any suggestions? I only have 4 bytes to work with to stay backward
compatible.

Thanks,
Steve

Nov 5 '07 #4
"Michael A. Covington" <mc@uga.eduwrote in message
news:ew****************@TK2MSFTNGP04.phx.gbl...
"Steve" <ki****@harpservices.comwrote in message
news:1b********************************@4ax.com...
>I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

Let's see. 4 bytes will hold an unsigned 32-bit integer. Convert the
date into number of days past some specific date, and store it as that.
Another possibility: Consider the date as a yyyymmdd string, for
instance, 20071231 (you can chhose a different format, of course, but this
one will be advantageous if you need to sort by date). Then store those 8
digits in your 4 bytes, 2 digits per byte. This will be easier to read than
"number of days from a specified date" if you ever have to look at a hex
dump of your data.
Nov 5 '07 #5
Hi Steve,

This article may give you some clues. It's not written in C#, but the principles are the same. Use >and << for bit manipulation.

[Bit Fields and Packed Data]
http://webster.cs.ucr.edu/AoA/Window...ntationa7.html
On Mon, 05 Nov 2007 18:57:51 +0100, Steve <ki****@harpservices.comwrote:
Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

When I do something like this,

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

I get a hex # 15 characters long.

Any suggestions? I only have 4 bytes to work with to stay backward
compatible.

Thanks,
Steve


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 5 '07 #6
Steve <ki****@harpservices.comwrote:
I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?
One thing no-one else has mentioned: strings aren't comprised of bytes.
They're comprised of *characters*. Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?

If you can use 4 bytes in an arbitrary fashion, using an int is likely
to be the best solution. 4 characters would be a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 5 '07 #7
Something like this?

int myDate = 20071105;
byte[] b = BitConverter.GetBytes(myDate);
Console.WriteLine(b.Length.ToString() + " bytes."); // reads "4 bytes"
-- Peter
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Steve" wrote:
Hi All,

I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

When I do something like this,

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

I get a hex # 15 characters long.

Any suggestions? I only have 4 bytes to work with to stay backward
compatible.

Thanks,
Steve
Nov 5 '07 #8
Hi Jon,
Nice catch :)

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Steve <ki****@harpservices.comwrote:
>I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

One thing no-one else has mentioned: strings aren't comprised of bytes.
They're comprised of *characters*. Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?

If you can use 4 bytes in an arbitrary fashion, using an int is likely
to be the best solution. 4 characters would be a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 5 '07 #9
On 2007-11-05 10:37:47 -0800, Jon Skeet [C# MVP] <sk***@pobox.comsaid:
Steve <ki****@harpservices.comwrote:
>I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

One thing no-one else has mentioned: strings aren't comprised of bytes.
That's true.
They're comprised of *characters*.
That's not. :) A string may _comprise_ characters. It's not
_comprised of_ characters, it's _composed of_ characters.

Sorry, pet peeve. But if people keep misusing "comprise" like that,
we're left with a word that used to be a perfectly useful word but
which is now just a useless synonym for "compose".

I hope your editor doesn't let you get away with stuff like that in
your book. :)
Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?
Now that is a very good point. As is sometimes the case, I and others
have fallen into the trap of assuming the OP has asked a question that
correctly reflects what he really wants to do. But you're right, his
question is ambigious and he should clarify whether he has 4 bytes or 4
characters to use.

Interestingly, if the latter then my personal opinion is that his
options are possibly actually more restricted than if he had 4 bytes.
Though the native .NET String format is 2 bytes per character,
typically one would not want to store invalid characters or characters
that wouldn't be correctly represented in some external storage.

So unless he could guarantee the string (assuming it is really a
string) is stored as some Unicode format, he'd want to limit himself to
at least regular, printable 8-bit characters (e.g. the Windows "ANSI"
or similar), if not to the printable subset of 7-bit ASCII. That
significantly would reduce the possible number of combinations
available below what a regular 32-bit binary data value would allow.

Ah...nothing is ever as simple as it first looks. :)

Pete

Nov 5 '07 #10
Jon, if this (below) converts back - and - forth:

int myDate = 20071105;
byte[] b = BitConverter.GetBytes(myDate);
Console.WriteLine(b.Length.ToString() + " bytes.");
// reads "4 bytes"
// And -back again:
Console.WriteLine("Orignal: " +BitConverter.ToInt32(b,
0).ToString());

-- then converting a dateTime object eg '11/05/2007' (US)
to the corresponding integer above is simple. Yes?
Peter
--
http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Jon Skeet [C# MVP]" wrote:
Steve <ki****@harpservices.comwrote:
I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

One thing no-one else has mentioned: strings aren't comprised of bytes.
They're comprised of *characters*. Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?

If you can use 4 bytes in an arbitrary fashion, using an int is likely
to be the best solution. 4 characters would be a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 5 '07 #11
On 2007-11-05 11:29:00 -0800, Peter Bromberg [C# MVP]
<pb*******@yahoo.NoSpamMaam.comsaid:
Jon, if this (below) converts back - and - forth:

int myDate = 20071105;
byte[] b = BitConverter.GetBytes(myDate);
Console.WriteLine(b.Length.ToString() + " bytes.");
// reads "4 bytes"
// And -back again:
Console.WriteLine("Orignal: " +BitConverter.ToInt32(b,
0).ToString());

-- then converting a dateTime object eg '11/05/2007' (US)
to the corresponding integer above is simple. Yes?
Storing a date in a 32-bit value is simple regardless, whether you use
the encoding you've demonstrated or some other. That's not the point
of Jon's post. The question Jon raises is that it's possible that the
OP has 4 characters, not 4 bytes, in which to store the data.
Depending on how those characters are stored, the OP could have more
flexibility or less as compared to a 32-bit data field.

Of course, if it really is a 32-bit data field in which the OP can
store the data, then the character question is moot. But we don't know
whether that's the case or not, until the OP clarifies his scenario.

Pete

Nov 5 '07 #12
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
They're comprised of *characters*.

That's not. :) A string may _comprise_ characters. It's not
_comprised of_ characters, it's _composed of_ characters.

Sorry, pet peeve. But if people keep misusing "comprise" like that,
we're left with a word that used to be a perfectly useful word but
which is now just a useless synonym for "compose".

I hope your editor doesn't let you get away with stuff like that in
your book. :)
That's what I get for posting with my son anxious to play a Lego City
game :)
Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?

Now that is a very good point. As is sometimes the case, I and others
have fallen into the trap of assuming the OP has asked a question that
correctly reflects what he really wants to do. But you're right, his
question is ambigious and he should clarify whether he has 4 bytes or 4
characters to use.

Interestingly, if the latter then my personal opinion is that his
options are possibly actually more restricted than if he had 4 bytes.
Though the native .NET String format is 2 bytes per character,
typically one would not want to store invalid characters or characters
that wouldn't be correctly represented in some external storage.
Exactly. It's interesting that a larger storage option is more
restrictive. It really depends on what's going to happen to it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 5 '07 #13
Yes, understood. When we look at the OP's code sample:

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

-- he is looking for a way to get a string (even if a HEX string).

I believe it is reasonably clear that because he is using an overload of
"ToString" that means (to him) somehow a character in a string is equivalent
to a byte.
--

Storing a date in a 32-bit value is simple regardless, whether you use
the encoding you've demonstrated or some other. That's not the point
of Jon's post. The question Jon raises is that it's possible that the
OP has 4 characters, not 4 bytes, in which to store the data.
Depending on how those characters are stored, the OP could have more
flexibility or less as compared to a 32-bit data field.

Of course, if it really is a 32-bit data field in which the OP can
store the data, then the character question is moot. But we don't know
whether that's the case or not, until the OP clarifies his scenario.

Pete

Nov 5 '07 #14
On Mon, 5 Nov 2007 18:37:47 -0000, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>Steve <ki****@harpservices.comwrote:
>I have a registration code that currently has 4 bytes that are unused.
I'd like to store a future date in those 4 bytes. Is there a way to
convert a date to a 4 byte string?

One thing no-one else has mentioned: strings aren't comprised of bytes.
They're comprised of *characters*. Now, do you need a 4-byte data
structure of any description, or an actual 4 character string?

If you can use 4 bytes in an arbitrary fashion, using an int is likely
to be the best solution. 4 characters would be a different matter.
Sorry I wasn't more clear. In my original post, I did mean bytes, not
characters. However, I later found that I had made a mistake and I
was looking at 4 bits; not 4 bytes. I doubt very seriously if anyone
can think of a way to store a date in 4 bits. Still, thanks to
everyone for their suggestions.

The only option I can now see is to modify the structure of our
original registration code. So much for backward compatibility. :-(

Steve
Nov 5 '07 #15
On 2007-11-05 12:06:00 -0800, Peter Bromberg [C# MVP]
<pb*******@yahoo.NoSpamMaam.comsaid:
Yes, understood. When we look at the OP's code sample:

string stringDate =
DateTime.Parse("1/15/2210").ToBinary().ToString("X");

-- he is looking for a way to get a string (even if a HEX string).

I believe it is reasonably clear that because he is using an overload of
"ToString" that means (to him) somehow a character in a string is equivalent
to a byte.
"Reasonably clear" is in the eye of the beholder. Suffice to say, I
disagree that anything about the original question is actually
"reasonably clear", in the sense that it's unambiguous. If anything,
the things you point to as making the question "reasonably clear" are,
in combination with the rest of what he wrote, the things that to me
prevent the question from being "reasonably clear".

Taken by themselves, you could make the assumptions you've made. But
taking the post as a whole, there are too many inconsistencies for the
question to be "reasonably clear".

Pete

Nov 5 '07 #16

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

Similar topics

1
by: Jim | last post by:
Can someone tell me how the following date and time is created to the Hex value (also shown below)? Tuesday, July 08, 2003 10:56:38 AM hex:00,90,a6,7b,66,45,c3,01 I need to be able to...
6
by: kevin carter | last post by:
hi i have a table conataining several fields one of which is date i want to be able to search the table on the date field using code. the code below generates the query from a form, however i get...
6
by: Mike Koerner | last post by:
Hi, I am having problems setting the HttpWebRequest Date header. I understand that it is a restricted header and I do receive the "This header must be modified with the appropriate property." ...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
3
by: yxq | last post by:
Hello, The XP Desktop clean wizard can get the last access time of desktop shortcut, i found that the info come from ...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
11
by: jwf | last post by:
I am writing a NON MFC C++ application (Plug-in for a 3rd party DB system). The application is designed to synchronise data between MS Outlook and the DB system. I am using smart pointers for the...
0
by: Alexf2 | last post by:
Hello! I'm reading binary file exported from mainframe's tape. There used COBOL data types. Some date fields are represented by PIC '9999', but some - by FIXED(7). The first one I have recognized...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...

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.