Convert dateTime components to hex then byte[]
-
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);
} -
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);
}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
-
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
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.
-
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.
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
-
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);
}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.
-
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
-
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.
-
Are the requirements fixed? A
DateTime
is really along
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) -
Are the requirements fixed? A
DateTime
is really along
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) -
Yes, the requirements are fixed. This is related to a chip layout that a contractor designed years ago. Thanks for the suggestions, though!
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) -
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)