Converting String to Hex

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
 
G

Guest

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.
 
G

Guest

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?
 
G

Guest

Dim str As String = "hello world"
Dim byteArray() As Byte

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

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top