Converting String to Hex

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This a rather simple question for all you studs out there! Please help me
with this. I have a string = "Please help me", and I want to convert this
into it's hex equivalent. How do I do it, I have trying for a couple hours
and can't seem to get my head straight arount this whole conversion things.

TIA, LCD
 
Not sure exactly what you want to do but check out the Hex Function, Convert
Class, and ToCharArray string method. One or all of these in combination
should get you what you want.
 
Okay I want to doing something similar to this. In python, there is a
function called "binascii".

So if I type:
###
This is the result I get:

'68656c6c6f20776f726c64'
###

I want to do something similar in VB.NET or C#. Can someone help?
 
Dim str As String = "hello world"
Dim byteArray() As Byte

byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
 
I just realized you are probably asking for the ascii values of each
character of a string in hex format. This should do it for you:

Dim str As String = "hello world"
Dim byteArray() As Byte
Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder

byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)

For i As Integer = 0 To byteArray.Length - 1
hexNumbers.Append(byteArray(i).ToString("x"))
Next

MessageBox.Show(hexNumbers.ToString())

The output should be:
68656c6c6f20776f726c64
 
Dude, you are genius!

You made it like POC (piece of cake). Now please explain me this, I didn't
quite catch this part:

hexNumbers.Append(byteArray(i).ToString("x"))

where is this "x" coming from? and why?

TIA, LCD
 
Back
Top