Board index » Visual Studio » Convext HEx value to byte array

Convext HEx value to byte array

Visual Studio50
Hi all ,





I have VB 6 application which request HEX value from hardware . I

need to convert the HEX value to the byte array .



Any ideas or suggestion how to do conversion ?

The HEX VALUE is : 18 3E 16 00 00 00 00 00

Thanks


-
 

Re:Convext HEx value to byte array

Quote
I have VB 6 application which request HEX value from hardware . I

need to convert the HEX value to the byte array .



Any ideas or suggestion how to do conversion ?

The HEX VALUE is : 18 3E 16 00 00 00 00 00

Thanks



It seems like there should be a simpler answer than this; but, since no

one has answered you yet, here it goes.



Function Hex2Byte(ByVal HexValue As String) As Byte()

Dim X

Dim ByteArray() As Byte

HexValue = Replace(HexValue, " ", "")

ReDim ByteArray((Len(HexValue) \ 2) - 1)

For X = 0 To UBound(ByteArray) - 2

ByteArray(X) = CLng("&H" & Mid$(HexValue, 2 * X + 1, 2))

Next

Hex2Byte = ByteArray

End Function



You can pass the HexValue in with the individual bytes separated by

spaces or not. Hence, both of these function calls will return the same

Byte array (using your example value).



Hex2Byte("18 3E 16 00 00 00 00 00")

Hex2Byte("183E160000000000")



Rick - MVP



-

Re:Convext HEx value to byte array

weetat wrote:

Quote


Hi all ,



I have VB 6 application which request HEX value from hardware . I

need to convert the HEX value to the byte array .



Any ideas or suggestion how to do conversion ?

The HEX VALUE is : 18 3E 16 00 00 00 00 00

Thanks



In what form is the input--ASCII? If so,



?val("&H"&"18" )

24

-

Re:Convext HEx value to byte array

"weetat" <test@wavex-tech.com>wrote in message

Quote
Hi all ,





I have VB 6 application which request HEX value from hardware . I

need to convert the HEX value to the byte array .



Any ideas or suggestion how to do conversion ?

The HEX VALUE is : 18 3E 16 00 00 00 00 00



Is the hardware providing an ascii string as shown including spaces?

Are those the byte values that you have read into a string?

Are they just the byte values that you expect?

Is this coming in over a COM port, LAN, USB, ???

Can you show the code that you are using to get the HEX VALUE?



you haven't provided nearly enough information for anybody to provide a

meaningful answer



-

Re:Convext HEx value to byte array



"Bob Butler" <tiredofit@nospam.com>wrote



Quote
>Any ideas or suggestion how to do conversion ?

>The HEX VALUE is : 18 3E 16 00 00 00 00 00



you haven't provided nearly enough information for anybody to provide a

meaningful answer





When will they learn?



<g>

LFS

-

Re:Convext HEx value to byte array

Hi Bob ,



Sorry for late reply



1 . Not including spaces

2 . Yes

3 . Yes

4 . COM port

5 . Sorry , cannot , using ActiveX DLL from our partner , confidential code



Thanks



Bob Butler wrote:

Quote
"weetat" <test@wavex-tech.com>wrote in message

news:%233vhYCavEHA.1520@TK2MSFTNGP11.phx.gbl



>Hi all ,

>

>

>I have VB 6 application which request HEX value from hardware . I

>need to convert the HEX value to the byte array .

>

>Any ideas or suggestion how to do conversion ?

>The HEX VALUE is : 18 3E 16 00 00 00 00 00





Is the hardware providing an ascii string as shown including spaces?

Are those the byte values that you have read into a string?

Are they just the byte values that you expect?

Is this coming in over a COM port, LAN, USB, ???

Can you show the code that you are using to get the HEX VALUE?



you haven't provided nearly enough information for anybody to provide a

meaningful answer



-

Re:Convext HEx value to byte array

"weetat" <test@wavex-tech.com>wrote in message

Quote
Hi Bob ,



Sorry for late reply





Thanks



Bob Butler wrote:

>"weetat" <test@wavex-tech.com>wrote in message

>news:%233vhYCavEHA.1520@TK2MSFTNGP11.phx.gbl

>

>>Hi all ,

>>

>>

>>I have VB 6 application which request HEX value from hardware . I

>>need to convert the HEX value to the byte array .

>>

>>Any ideas or suggestion how to do conversion ?

>>The HEX VALUE is : 18 3E 16 00 00 00 00 00

>

>Is the hardware providing an ascii string as shown including spaces?



1 . Not including spaces



>Are those the byte values that you have read into a string?



2 . Yes

4 . COM port

5 . Sorry , cannot , using ActiveX DLL from our partner ,

confidential code



>Are they just the byte values that you expect?



3 . Yes

>Is this coming in over a COM port, LAN, USB, ???

>Can you show the code that you are using to get the HEX VALUE?

>

>you haven't provided nearly enough information for anybody to

>provide a meaningful answer



--

Reply to the group so all can participate

VB.Net... just say "No"



-

Re:Convext HEx value to byte array

"weetat" <test@wavex-tech.com>wrote in message

Quote
Hi Bob ,



Sorry for late reply





Thanks



Bob Butler wrote:

>"weetat" <test@wavex-tech.com>wrote in message

>news:%233vhYCavEHA.1520@TK2MSFTNGP11.phx.gbl

>

>>Hi all ,

>>

>>

>>I have VB 6 application which request HEX value from hardware . I

>>need to convert the HEX value to the byte array .

>>

>>Any ideas or suggestion how to do conversion ?

>>The HEX VALUE is : 18 3E 16 00 00 00 00 00

>

>Is the hardware providing an ascii string as shown including spaces?



1 . Not including spaces



>Are those the byte values that you have read into a string?



2 . Yes



>Are they just the byte values that you expect?



3 . Yes



>Is this coming in over a COM port, LAN, USB, ???



4 . COM port



>Can you show the code that you are using to get the HEX VALUE?



5 . Sorry , cannot , using ActiveX DLL from our partner ,

confidential code



>you haven't provided nearly enough information for anybody to

>provide a meaningful answer



Answers 2 & 3 are contradictory but I'm assuming I just didn't make question

3 clear enough... If you have that sequence as a string in VB then I think

you want something like this:



Private Sub Main()

Dim b() As Byte

b = Hexify("183E10000000000")

End Sub



Public Function Hexify(ByVal TheBytes As String) As Byte()

Dim b() As Byte

Dim x As Long

ReDim b(1 To Len(TheBytes) \ 2)

For x = 1 To UBound(b)

b(x) = CLng("&H" & Mid$(TheBytes, x * 2 - 1, 2))

Next

Hexify = b

End Function



--

Reply to the group so all can participate

VB.Net... just say "No"



-

Re:Convext HEx value to byte array

Quote
Public Function Hexify(ByVal TheBytes As String) As Byte()

Dim b() As Byte

Dim x As Long

ReDim b(1 To Len(TheBytes) \ 2)

For x = 1 To UBound(b)

b(x) = CLng("&H" & Mid$(TheBytes, x * 2 - 1, 2))

Next

Hexify = b

End Function



Why didn't I think of something along these lines? <g>



Rick

-

Re:Convext HEx value to byte array

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net>wrote in message

Quote
>Public Function Hexify(ByVal TheBytes As String) As Byte()

>Dim b() As Byte

>Dim x As Long

>ReDim b(1 To Len(TheBytes) \ 2)

>For x = 1 To UBound(b)

>b(x) = CLng("&H" & Mid$(TheBytes, x * 2 - 1, 2))

>Next

>Hexify = b

>End Function



Why didn't I think of something along these lines? <g>



I know you did... I'm still not clear on the question though.



--

Reply to the group so all can participate

VB.Net... just say "No"



-