I have string and want to convert it into hexadecimal in C#.net.
This is my Eset Nod32 password:
"12968"
The program saves this password into a binary registry key as:
"50 d6 e6 e9 e4 f0 cd f2 63 64"
How can I do this in C#?
I have string and want to convert it into hexadecimal in C#.net.
This is my Eset Nod32 password:
"12968"
The program saves this password into a binary registry key as:
"50 d6 e6 e9 e4 f0 cd f2 63 64"
How can I do this in C#?
If you want to get the HEX of each number, for example Hex of 1, Hex of 2 , etc. you can do as below:
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);
}
or if you want get the complete string as a float number for example this is the way :
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);
There can be used following to write binary value to registry
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);