Date to 4 byte string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    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
  • Michael A. Covington

    #2
    Re: Date to 4 byte string


    "Steve" <kilroy@harpser vices.comwrote in message
    news:1bkui3doo8 icfp0jjd805s6nc cmpqsil1f@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.


    Comment

    • Peter Duniho

      #3
      Re: Date to 4 byte string

      On 2007-11-05 09:57:51 -0800, Steve <kilroy@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

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Date to 4 byte string

        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
        The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

        Mobile & warehouse Solutions.
        "Steve" <kilroy@harpser vices.comwrote in message
        news:1bkui3doo8 icfp0jjd805s6nc cmpqsil1f@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

        Comment

        • Alberto Poblacion

          #5
          Re: Date to 4 byte string

          "Michael A. Covington" <mc@uga.eduwrot e in message
          news:ewJ%23$X9H IHA.1184@TK2MSF TNGP04.phx.gbl. ..
          "Steve" <kilroy@harpser vices.comwrote in message
          news:1bkui3doo8 icfp0jjd805s6nc cmpqsil1f@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.


          Comment

          • Morten Wennevik [C# MVP]

            #6
            Re: Date to 4 byte string

            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]



            On Mon, 05 Nov 2007 18:57:51 +0100, Steve <kilroy@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]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Date to 4 byte string

              Steve <kilroy@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 - <skeet@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

              Comment

              • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                #8
                RE: Date to 4 byte string

                Something like this?

                int myDate = 20071105;
                byte[] b = BitConverter.Ge tBytes(myDate);
                Console.WriteLi ne(b.Length.ToS tring() + " bytes."); // reads "4 bytes"
                -- Peter

                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
                >

                Comment

                • Ignacio Machin \( .NET/ C# MVP \)

                  #9
                  Re: Date to 4 byte string

                  Hi Jon,


                  Nice catch :)

                  --
                  Ignacio Machin
                  The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

                  Mobile & warehouse Solutions.
                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:MPG.219973 559c4994995ca@m snews.microsoft .com...
                  Steve <kilroy@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 - <skeet@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

                  Comment

                  • Peter Duniho

                    #10
                    Re: Date to 4 byte string

                    On 2007-11-05 10:37:47 -0800, Jon Skeet [C# MVP] <skeet@pobox.co msaid:
                    Steve <kilroy@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

                    Comment

                    • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                      #11
                      Re: Date to 4 byte string

                      Jon, if this (below) converts back - and - forth:

                      int myDate = 20071105;
                      byte[] b = BitConverter.Ge tBytes(myDate);
                      Console.WriteLi ne(b.Length.ToS tring() + " bytes.");
                      // reads "4 bytes"
                      // And -back again:
                      Console.WriteLi ne("Orignal: " +BitConverter.T oInt32(b,
                      0).ToString());

                      -- then converting a dateTime object eg '11/05/2007' (US)
                      to the corresponding integer above is simple. Yes?
                      Peter
                      --

                      unBlog: http://petesbloggerama.blogspot.com
                      BlogMetaFinder: http://www.blogmetafinder.com



                      "Jon Skeet [C# MVP]" wrote:
                      Steve <kilroy@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 - <skeet@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
                      >

                      Comment

                      • Peter Duniho

                        #12
                        Re: Date to 4 byte string

                        On 2007-11-05 11:29:00 -0800, Peter Bromberg [C# MVP]
                        <pbromberg@yaho o.NoSpamMaam.co msaid:
                        Jon, if this (below) converts back - and - forth:
                        >
                        int myDate = 20071105;
                        byte[] b = BitConverter.Ge tBytes(myDate);
                        Console.WriteLi ne(b.Length.ToS tring() + " bytes.");
                        // reads "4 bytes"
                        // And -back again:
                        Console.WriteLi ne("Orignal: " +BitConverter.T oInt32(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

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Date to 4 byte string

                          Peter Duniho <NpOeStPeAdM@Nn OwSlPiAnMk.comw rote:
                          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 - <skeet@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

                          Comment

                          • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

                            #14
                            Re: Date to 4 byte string

                            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
                            >
                            >

                            Comment

                            • Steve

                              #15
                              Re: Date to 4 byte string

                              On Mon, 5 Nov 2007 18:37:47 -0000, Jon Skeet [C# MVP]
                              <skeet@pobox.co mwrote:
                              >Steve <kilroy@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.
                              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

                              Comment

                              Working...