473,748 Members | 6,370 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4369

"Steve" <ki****@harpser vices.comwrote in message
news:1b******** *************** *********@4ax.c om...
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****@harpser vices.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****@harpser vices.comwrote in message
news:1b******** *************** *********@4ax.c om...
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.eduwrot e in message
news:ew******** ********@TK2MSF TNGP04.phx.gbl. ..
"Steve" <ki****@harpser vices.comwrote in message
news:1b******** *************** *********@4ax.c om...
>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****@harpser vices.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****@harpser vices.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.co m>
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.Ge tBytes(myDate);
Console.WriteLi ne(b.Length.ToS tring() + " 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.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Steve <ki****@harpser vices.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.co m>
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.co msaid:
Steve <ki****@harpser vices.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

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

Similar topics

1
10638
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 create the hex value for any date, but am not sure how to break it down. Serail Parts?
6
2763
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 an error message "Run time Error 2001" when this code is run. Can anyone please tell me where i have gone wrong or how to stop this error message Dim db As DAO.Database Dim qdf As DAO.QueryDef
6
4198
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." Is there a way to make sure that the date header is sent over or a way to work around this exception? Here's a sample of the code:
29
9120
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 example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and have not really found an answer. I know that Leap Years need to be accounted for too. Any...
3
2009
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 "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{75048700-EF1F-11D0-9888-006097DEACF9}\Count" The valuename and value are encrypted using ROT13, the function below can decrypt them *************************************************************
8
4200
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
7
2770
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, here is my table structure, Name: Table1
11
5272
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 development which is fine and sysnchronisation back and forth is working as expected. The problem I am having is with the DATE type (implemented using an 8-byte floating-point number. Days are represented by whole number increments starting with...
0
1129
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 and converted quickly, but the second one is a problem. I found the conversion algorithm (http://www.thescripts.com/forum/thread267116.html) to convert FIXED(7) to UInt32 value, but now I don't know how to construct .Net DateTime from this UInt32....
0
8991
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, 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...
0
8830
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
9544
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...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6796
isladogs
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...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.