Question unicode to hex

skitz

Member
Joined
May 8, 2009
Messages
15
Programming Experience
Beginner
Ok so i have another quiestion about hex string's.....

i have a hex string i need to be shown in a textbox, but i seem to be having trouble i can get the unicode string in the textbox but not the other way round can someone help please

to get the unicode im using

VB.NET:
 textbox.Text = UnicodeEncoding.Unicode.GetString(Text)
(i am using other code to get the offset etc)

so as you can see i can get the unicode but how do i get the hex string to the textbox

thanks
 
First up, that code is wrong. The Encoding.Unicode property returns a UnicodeEncoding object, so that code should either be:
VB.NET:
textbox.Text = Encoding.Unicode.GetString(Text)
or:
VB.NET:
textbox.Text = New UnicodeEncoding.GetString(Text)
The only reason it works as is is because the IDE kindly fixes your error for you when it compiles.

As for getting the hex value of each character:
VB.NET:
For Each ch As Char In textbox.Text
    MessageBox.Show(Convert.ToInt32(ch).ToString("X2"))
Next
You can do whatever you like with the byte strings. Showing them in a message box is just an example. Note also that X2 requires every character to have a value of 255 or less. If there are any 2-byte characters in that string then you'd need to use X4.
 
Back
Top