Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Convert dateTime components to hex then byte[]

Convert dateTime components to hex then byte[]

Scheduled Pinned Locked Moved C#
data-structurestutorial
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MichCl

    I'm having a tough time converting month, day, and year into hex then byte[]. I did an internet search and I'm not finding anything that is similar enough to provide the link to. This is what I have so far from before I realized I needed the hex, but I do need the 2-digit mo/day/yr before it's hex. I'm a little confused about converting to hex since I would normally take the number and use ToString("X"), but it doesn't seem to apply here since I'm applying the formatting to get it to the 2-digit month, for example.

    DateTime mfg = DateTime.Now;

    mfgDate_mo = new byte[mfg.ToString("MM").Length * sizeof(char)];//month
    convertStringToByteArr(mfg.ToString("MM"), ref mfgDate_mo);
    mfgDate_dy = new byte[mfg.ToString("dd").Length * sizeof(char)];//day
    convertStringToByteArr(mfg.ToString("dd"), ref mfgDate_dy);
    mfgDate_yr = new byte[mfg.ToString("yy").Length * sizeof(char)];//year
    convertStringToByteArr(mfg.ToString("yy"), ref mfgDate_yr);

    //convert string to byte array, not taking encoding into account
    private void convertStringToByteArr(String theString, ref byte[] theByteArr)
    {
    System.Buffer.BlockCopy(theString.ToCharArray(), 0, theByteArr, 0, theByteArr.Length);
    }

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    I'm sorry, but your description doesn't make much sense to me. I've read it a couple of times and I can't visualise what you are trying to do. Given an input of todays date, what are you expecting to see in the byte array? If you could illustrate this, then perhaps I can help.

    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    M 1 Reply Last reply
    0
    • P Pete OHanlon

      I'm sorry, but your description doesn't make much sense to me. I've read it a couple of times and I can't visualise what you are trying to do. Given an input of todays date, what are you expecting to see in the byte array? If you could illustrate this, then perhaps I can help.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      M Offline
      M Offline
      MichCl
      wrote on last edited by
      #3

      For example, today's date is 8/20/2012. I need to take the 8, represent it as 08, convert it to Hex which is still 08 for that one, then put it in a byte array. For the year, I need to take the 12, which is already 2 digits, then convert to hex, which is 0C, then put it in byte array.

      P 1 Reply Last reply
      0
      • M MichCl

        For example, today's date is 8/20/2012. I need to take the 8, represent it as 08, convert it to Hex which is still 08 for that one, then put it in a byte array. For the year, I need to take the 12, which is already 2 digits, then convert to hex, which is 0C, then put it in byte array.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #4

        So just use the following to convert:

        private byte[] Convert(int datePart)
        {
        // First of all, convert the date part into hex.
        string convertedPart = datePart.ToString("X2");
        // Now, return the byte array for the part.
        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        return enc.GetBytes(convertedPart);
        }

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        M 1 Reply Last reply
        0
        • M MichCl

          I'm having a tough time converting month, day, and year into hex then byte[]. I did an internet search and I'm not finding anything that is similar enough to provide the link to. This is what I have so far from before I realized I needed the hex, but I do need the 2-digit mo/day/yr before it's hex. I'm a little confused about converting to hex since I would normally take the number and use ToString("X"), but it doesn't seem to apply here since I'm applying the formatting to get it to the 2-digit month, for example.

          DateTime mfg = DateTime.Now;

          mfgDate_mo = new byte[mfg.ToString("MM").Length * sizeof(char)];//month
          convertStringToByteArr(mfg.ToString("MM"), ref mfgDate_mo);
          mfgDate_dy = new byte[mfg.ToString("dd").Length * sizeof(char)];//day
          convertStringToByteArr(mfg.ToString("dd"), ref mfgDate_dy);
          mfgDate_yr = new byte[mfg.ToString("yy").Length * sizeof(char)];//year
          convertStringToByteArr(mfg.ToString("yy"), ref mfgDate_yr);

          //convert string to byte array, not taking encoding into account
          private void convertStringToByteArr(String theString, ref byte[] theByteArr)
          {
          System.Buffer.BlockCopy(theString.ToCharArray(), 0, theByteArr, 0, theByteArr.Length);
          }

          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #5

          Why don't you just do binary serialization to a byte array? It'll be a little bigger then what you are doing because of the meta data, but if you want it really light weight, I don't really get the hex step. Just store the month, day and year?? Also, don't store just the 2 digit yr or you'll have Y2K issues.

          M 1 Reply Last reply
          0
          • P Pete OHanlon

            So just use the following to convert:

            private byte[] Convert(int datePart)
            {
            // First of all, convert the date part into hex.
            string convertedPart = datePart.ToString("X2");
            // Now, return the byte array for the part.
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            return enc.GetBytes(convertedPart);
            }

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            M Offline
            M Offline
            MichCl
            wrote on last edited by
            #6

            Thanks! I'll give it a try. It seems like it will work with adding a conversion from DateTime to integer.

            int mfgDate_mo_int = mfg.Month;

            1 Reply Last reply
            0
            • S SledgeHammer01

              Why don't you just do binary serialization to a byte array? It'll be a little bigger then what you are doing because of the meta data, but if you want it really light weight, I don't really get the hex step. Just store the month, day and year?? Also, don't store just the 2 digit yr or you'll have Y2K issues.

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #7

              I have to convert to hex and do 2-digits because of system requirements.

              D 1 Reply Last reply
              0
              • M MichCl

                I have to convert to hex and do 2-digits because of system requirements.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #8

                Are the requirements fixed? A DateTime is really a long internally (Ticks), which of course can be stored in/retrieved from 8 bytes very easily.

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                M 1 Reply Last reply
                0
                • D DaveyM69

                  Are the requirements fixed? A DateTime is really a long internally (Ticks), which of course can be stored in/retrieved from 8 bytes very easily.

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  M Offline
                  M Offline
                  MichCl
                  wrote on last edited by
                  #9

                  Yes, the requirements are fixed. This is related to a chip layout that a contractor designed years ago. Thanks for the suggestions, though!

                  D 1 Reply Last reply
                  0
                  • M MichCl

                    Yes, the requirements are fixed. This is related to a chip layout that a contractor designed years ago. Thanks for the suggestions, though!

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #10

                    No problem. Did you get it working?

                    Dave
                    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    M 1 Reply Last reply
                    0
                    • D DaveyM69

                      No problem. Did you get it working?

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      M Offline
                      M Offline
                      MichCl
                      wrote on last edited by
                      #11

                      Yes. I still have to try writing the data to the chip to test it, but hopefully all is good (encoding) when I do.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups