Hexadecimal

In the code below, ans is declare as byte. It reads values from the registry and converts them to a string. However in the string the values are not returned as hexadecimal as they are in the registry instead they are converted to decimal (i.e. "23" instead of "17" and "245" instead of "f5"). Can anyone help me keep these in hexadecimal format? I appreciate the help. Thanks


ans = regKey.GetValue("F")
For i = 0To ans.Length - 1
' Create a string from the byte array
value = value + Convert.ToString(ans.GetValue(i)) + " "
Next

ListBox1.Items.Add(value)


[1161 byte] By [Justin5] at [2007-12-16]
# 1

Would something like this work?



Dim bytes As Byte() = New Byte() {1, 23, 245, 17}
Dim sb As New System.Text.StringBuilder
For Each b As Byte In bytes
sb.Append(b.ToString(
"x2"))
sb.Append(" ")
Next
ListBox1.Items.Add(sb.ToString())

The only other change that I made was to use a string builder instead of a string, since that is a much faster method of building a string like this. Using your original method of concatenating strings will produce the same output...

Best regards,
Johan Stenberg

JohanStenberg at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hello

I have a similar sort of problem, accept I wish to convert a string to hexadecimal, and then write it to a registry key.

I would like to use the string that is typed into : txtPassword

I can manage all the reading and writing registry stuff, I just cant figure out how to convert a string to hexadecimal!

Any ideas on this would be great.

Tim

grilledtomatoes at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Do you mean convert it to binary? Or do you mean ascii or UTF-8 character representing a hex string?

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...