Reputation: 228
I was trying to convert the datetime to hexadecimal. This is what I have
string hexValue = DateTime.Today.ToString("X")
I can not find the solution to this.
Upvotes: 2
Views: 11336
Reputation:
public static string ConvertStringToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
Upvotes: -1
Reputation: 3300
You can do following with C#.
DateTime dt = new DateTime();
dt = DateTime.Now;
//Convert date time format 20170710041800
string str = dt.ToString("yyyyMMddhhmmss");
//Convert to Long
long decValue = Convert.ToInt64(str);
//Convert to HEX 1245D8F5F7C8
string hexValue = decValue.ToString("X");
//Hex To Long again 20170710041800
long decAgain = Int64.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
Please mark as answer if it is helpfull to you
Upvotes: 1
Reputation: 4043
You can do:
string hexValue = DateTime.Now.Ticks.ToString("X2");
This will give you the hex
value.
To convert it back to DateTime
you do:
DateTime dateTime = new DateTime(Convert.ToInt64(hexValue, 16));
Upvotes: 14