Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Datetime转换为十六进制_C# - Fatal编程技术网

C# 如何将Datetime转换为十六进制

C# 如何将Datetime转换为十六进制,c#,C#,我试图将日期时间转换为十六进制。 这就是我所拥有的 string hexValue = DateTime.Today.ToString("X") 我找不到解决此问题的方法。您可以执行以下操作: string hexValue = DateTime.Now.Ticks.ToString("X2"); 这将为您提供十六进制值 要将其转换回DateTime,请执行以下操作: DateTime dateTime = new DateTime(Convert.ToInt64(hexValue, 16)

我试图将日期时间转换为十六进制。 这就是我所拥有的

string hexValue = DateTime.Today.ToString("X")
我找不到解决此问题的方法。

您可以执行以下操作:

string hexValue = DateTime.Now.Ticks.ToString("X2");
这将为您提供十六进制值

要将其转换回DateTime,请执行以下操作:

DateTime dateTime = new DateTime(Convert.ToInt64(hexValue, 16));
你可以做:

string hexValue = DateTime.Now.Ticks.ToString("X2");
这将为您提供十六进制值

要将其转换回DateTime,请执行以下操作:

DateTime dateTime = new DateTime(Convert.ToInt64(hexValue, 16));

你可以用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); 

如果对您有帮助,请将其标记为答案

您可以使用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); 
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;
}

如果对您有帮助,请将其标记为答案

您希望您的日期如何表示为十六进制?例如,今天2017年7月10日的十六进制是什么?为什么需要这个?@Jamiec yes likethat@PedroAzevedo-是的,像什么?我问了一个问题,没有提出解决方案!哈哈,十六进制编码就是加密。我喜欢!我希望你的用户不是学龄前儿童就是你不太在乎许可费。你希望你的日期怎么用十六进制表示?例如,今天2017年7月10日的十六进制是什么?为什么需要这个?@Jamiec yes likethat@PedroAzevedo-是的,像什么?我问了一个问题,没有提出解决方案!哈哈,十六进制编码就是加密。我喜欢!我希望您的用户不是学龄前儿童就是您不太关心许可证费用。这可能是对一个不太明智的问题最明智的回答。@Jamiec谢谢:这让我非常接近我的第一个标签0.0这可能是对一个不太明智的问题最明智的回答。@Jamiec谢谢:这让我非常接近我的目标第一个标签徽章0.0
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;
}