我試着編寫一個擴展方法來接收ulong並返回一個字符串,它表示以十六進制格式提供的沒有前導零的值。我對於自己的想法並不滿意......有沒有更好的方式來使用標準的.NET庫來實現這一點?代碼高爾夫:C#:將ulong轉換爲十六進制字符串
public static string ToHexString(this ulong ouid)
{
string temp = BitConverter.ToString(BitConverter.GetBytes(ouid).Reverse().ToArray()).Replace("-", "");
while (temp.Substring(0, 1) == "0")
{
temp = temp.Substring(1);
}
return "0x" + temp;
}
打我吧:o)另外,找到了這個。類似的問題: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/d14da7dc-6258-42a1-b660-d18bdf9635b8 – 2010-02-23 15:22:35
哈哈,真棒。返回「0x」+ ouid.ToString(「x」)。ToUpper(); <3 – Langdon 2010-02-23 15:24:28
我實際上會很有幫助...所以你可以跳過所有的BitConverter等。 return string.Format(「0x {0:x}」,temp); 如果你甚至想寫一個擴展名爲此目的(我不會):D – cyberzed 2010-02-23 15:24:54