# Hex to Binary

Hi,

Kindly help me out from the below case

I want to convert the Hex value to binary value.

Now I have stored the Hex value in one string , but unable to convert to binary the same.

Below listed my code:

Declared the Glb_data as String in Global variable;

Dim n As Integer
if n <= 0 then return

Try:

Hex
Dim hexstr as string = Hex( Val( “&b” + binstr))

To turn to binary:

Dim binstr as string = Bin( Val( “&h” + hexstr ))

octal would be &o so on and so forth.

You can transform the format back and forth from one to the next using the & symbol.

[quote=208024:@Matthew Combatti]Try:

Hex
Dim hexstr as string = Hex( Val( “&b” + binstr))

To turn to binary:

Dim binstr as string = Bin( Val( “&h” + hexstr ))

octal would be &o so on and so forth.[/quote]

Thanks… Working fine.

Any one advice how to store the binary values into array.

For example:

Binary data = 1111111101010101

Output should be like
Array(0) = 1
Array(1) = 1
Array(2) = 1
.
.
.
Array(15) = 1

Well if Array is a string then

``Array = Split(<Binary Data>, "")``

however if Array is an integer then you’ll need to get the val of the data

```For i As Integer = 1 To LenB(<Binary Data>) Array.append Val(Mid(<Binary Data>, i, 1)) Next```

[quote=208040:@Wayne Golding]Well if Array is a string then

``Array = Split(<Binary Data>, "")``

however if Array is an integer then you’ll need to get the val of the data

```For i As Integer = 1 To LenB(<Binary Data>) Array.append Val(Mid(<Binary Data>, i, 1)) Next```[/quote]

My code as follows:

Dim Serial_Array(16) As String

if n <= 0 then return

Dim hexstr as string = Bin( Val( “&h” + Glb_Data ))

Serial_Array = Split(hexstr, " ")

Values are not stored in array’s

``Serial_Array = Split(hexstr, "")   // empty string !!!``

As @Eli Ott says there are no spaces in your string, but there is an empty string between each character Thanks for all.

[quote=208024:@Matthew Combatti]Try:

Hex
Dim hexstr as string = Hex( Val( “&b” + binstr))

To turn to binary:

Dim binstr as string = Bin( Val( “&h” + hexstr ))

octal would be &o so on and so forth.[/quote]

Hi Matthew,
I have using below comment to convert hex to bin

“Dim binstr as string = Bin( Val( “&h” + hexstr ))”

Its working fine normally. But my problem is ,

For Example my Hex value is “8100” means the binary output should be “0000000010000001”.

But above code provide the output as “10000001”

I want the output as 16 digit format.

anyone help me out from this.

``Dim binstr as string = RIGHT("0000000000000000"+Bin( Val( "&h" + hexstr )),16)``

Put 16 zeros as Dave said. There are 8 1’s or 0’s (bits) in a single byte.

00000000 is 1 byte The 16 0’s permits overflow if say the lowest possible return of only a single 1 or 0 was returned in the hex to bin conversion.