From: kronecker on
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.




From: Martin H. on
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

On 25.06.2008 15:38, wrote kronecker(a)yahoo.co.uk:
> 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.
>
>
>
>

From: kronecker on
On Jun 26, 12:56 am, "Martin H." <hk...(a)gmx.net> wrote:
> 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
>
> On 25.06.2008 15:38, wrote kronec...(a)yahoo.co.uk:
>
> > 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.

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

k.
From: Martin H. on
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