2014-01-25 44 views
-1

我有字符串,並希望在C#.net中將其轉換爲十六進制。如何在C#中將字符串轉換爲十六進制.net

這是我的ESET NOD32密碼:

"12968" 

程序將保存此密碼爲二進制註冊表項:

"50 d6 e6 e9 e4 f0 cd f2 63 64" 

我怎樣才能做到這在C#?

+1

什麼是規則映射'12968'到'50 d6 e6 e9 e4 f0 cd f2 63 64'。我沒有看到任何關係。 –

+0

您有兩個問題,這兩個問題已經被回答。請使用搜索。請參閱[將字符串值轉換爲十六進制十進制](http://stackoverflow.com/questions/8739577/converting-string-value-to-hex-decimal)和[如何向註冊表寫入二進制數據(如何)(http ://stackoverflow.com/questions/5087240/how-to-write-binary-data-as-is-to-registry-ie-i-have-visible-binary-data-as)。 – CodeCaster

+0

顯然不是這麼簡單的關係。十六進制有80位,輸入要小得多。這可能是某種散列或者某些東西來阻止你完成你正在嘗試做的事情。 – harold

回答

0

如果你想獲得每個數字的十六進制數字,1例十六進制,2進制,等等。你可以如下操作:

string input = "12968"; 
char[] values = input.ToCharArray(); 
foreach (char letter in values) 
{ 
    // Get the integral value of the character. 
    int value = Convert.ToInt32(letter); 
    // Convert the decimal value to a hexadecimal value in string form. 
    string hexOutput = String.Format("{0:X}", value); 
    Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput); 
} 

,或者如果你想獲得完整的字符串作爲例如該浮點數的方式是:

string hexString = "12968"; 
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier); 

byte[] floatVals = BitConverter.GetBytes(num); 
float f = BitConverter.ToSingle(floatVals, 0); 
Console.WriteLine("float convert = {0}", f); 
+0

感謝兄弟。當我使用你的代碼。 「12968」的輸出爲「31 32 39 36 38」,但是。我想轉換爲這個樣式「50 d6 e6 e9 e4 e4 f0 cd f2 63 64」 – MiladCoder

+0

僅通過轉換爲十六進制,就無法從輸入中獲取該值(「50 d6 e6 e9 e4 e4 f0 cd f2 63 64」)。很可能你的NOD32對你的密碼做了一些散列或加密 –

0

可以使用下面寫入二進制值對註冊表

static byte[] GetBytes(string str) 
{ 
    byte[] bytes = new byte[str.Length * sizeof(char)]; 
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); 
    return bytes; 
} 

RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistryValueKindExample"); 
rk.SetValue("BinaryValue", GetBytes("12968"), RegistryValueKind.Binary); 

RegistryKey.SetValue Method

+0

感謝兄弟。但不是這種風格:「50 d6 e6 e9 e4 f4 cd f2 63 64」yet :( – MiladCoder

+0

這是nod32映射。我的EsetNod32密碼是:12968。我導航到Regedit並查看「50 d6 e6 e9 e4 e4 f0 cd f2 63 64 「password for password。現在我想設置我的新密碼,但我必須重新創建這樣的值。 – MiladCoder

相關問題