HOWTO: Calculating and Summing HEX values

  • Thread starter Thread starter Filippo
  • Start date Start date
F

Filippo

Hi Gurus,
I have a date and I need to convert it into a HEX value with a checksum.

SCENARIO
===================
Starting point:
DateTime aDate = new DateTime(2003,05,29)

Expected Result:
07D3051DFC

Where the values represent:
07D3 the year
05 the month
1D the day
FC the sum of the above values (yyyy should be summed separating the first two bytes
from the second two bytes)


....like this ==> (yy + yy + mm + dd)

Can any C# guru provide me with a simple function that can take a date and create the expected HEX?

Many thanks in advanced,
Filippo.
 
:)

=============================
private string CreateNewAuthorization()
{
string aStr;

string aYear1;
string aYear2;
string aMonth;
string aDay;
long aChkSum = 0;

DateTime aDate = new DateTime(2003,05,29);

aYear1 = aDate.Year.ToString("X").PadLeft(4,'0').Substring(0,2);
aYear2 = aDate.Year.ToString("X").PadLeft(4,'0').Substring(2,2);
aMonth = aDate.Month.ToString("X").PadLeft(2,'0');
aDay = aDate.Day.ToString("X").PadLeft(2,'0');

aStr = aYear1 + aYear2 + aMonth + aDay;
try
{
aChkSum = Convert.ToInt32(("0x" + aYear1), 16);
aChkSum = aChkSum + Convert.ToInt32(("0x" + aYear2), 16);
aChkSum = aChkSum + Convert.ToInt32(("0x" + aMonth), 16);
aChkSum = aChkSum + Convert.ToInt32(("0x" + aDay), 16);
}
catch (Exception ex)
{
return ex.Message;
}

// add the checksum of --> add HEX pairs (split year)
aStr = aStr + aChkSum.ToString("X");

return aStr;
}




Hi Gurus,
I have a date and I need to convert it into a HEX value with a checksum.

SCENARIO
===================
Starting point:
DateTime aDate = new DateTime(2003,05,29)

Expected Result:
07D3051DFC

Where the values represent:
07D3 the year
05 the month
1D the day
FC the sum of the above values (yyyy should be summed separating the first two bytes
from the second two bytes)


....like this ==> (yy + yy + mm + dd)

Can any C# guru provide me with a simple function that can take a date and create the expected HEX?

Many thanks in advanced,
Filippo.
 
Back
Top