Hex to String

K

kronecker

I have a Hex number say AEFF that I need as a string for the serial
port. How to convert it?

Will (strCmd isa string)

strCmd=&AEFF

do the job or is it

strCmd="&AEFF"

or

strCmd=Cstr("&AEFF")

Also to do similar from binary is it

strCmd="10101011101011" etc

?

Thanks

K.
 
M

Martin H.

Hello Kronecker,

I understand that you currently have the numeric value, right?
So let's say

Dim HexValue As Integer = &HAEFF
Dim HexString As String = Hex(HexValue)

To convert from the Hex string to binary string, you could do this:

Dim BinaryString As String = ""
Dim t As Integer
For t = 1 To Len (HexString)
Select Case Strings.Mid(HexString,t,1)
Case "0"
BinaryString &="0000"

Case "1"
BinaryString &="0001"

Case "2"
BinaryString &="0010"

' ... and so on until F

End Select
Next

Best regards,

Martin
 
K

kronecker

Hello Kronecker,

I understand that you currently have the numeric value, right?
So let's say

Dim HexValue As Integer = &HAEFF
Dim HexString As String = Hex(HexValue)

To convert from the Hex string to binary string, you could do this:

Dim BinaryString As String = ""
Dim t As Integer
For t = 1 To Len (HexString)
Select Case Strings.Mid(HexString,t,1)
Case "0"
BinaryString &="0000"

Case "1"
BinaryString &="0001"

Case "2"
BinaryString &="0010"

' ... and so on until F

End Select
Next

Best regards,

Martin

Thanks for that. Great idea. Should that be
For t = 1 To Len (HexString)
or
For t = 0 To Len (HexString)-1
?

k.
 
M

Martin H.

Hello Kronecker,
Thanks for that. Great idea. Should that be
For t = 1 To Len (HexString)
or
For t = 0 To Len (HexString)-1

Strings.Mid is a classic BASIC way and expects values of 1 and greater.
So it would be For t = 1 To Len (HexString).

If you want to use the .NET way, it would be like this:
For t = 0 To Len (HexString)-1
Select Case HexString.Substring(t,1)

Best regards,

Martin
 

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