newprint
newprint

Reputation: 7136

DateTime to Hex and reverse without conversion to Int64

I need to convert DateTime to Hex string, as Hex string to DateTime.

My conversion from DateTime to Hex Convert.ToInt64(dt.ToString("yyyyMMddhhmmss")).ToString("X2") converts DateTime to String to Int64 to String.
Is it possible to simplify this method by avoiding multiple conversion ?

Upvotes: 1

Views: 479

Answers (1)

Habib
Habib

Reputation: 223282

Use DateTime.Ticks which is Int64/long and that you can use for Hex string like:

string hexString = dt.Ticks.ToString("X2");

To get the DateTime back you can do:

DateTime dt1 = new DateTime(Convert.ToInt64(hexString, 16));

Upvotes: 3

Related Questions