Function that convert Hexadecimal to Binary?

G

Guest

I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
 
J

Jay B. Harlow [MVP - Outlook]

MrKrich,
You can use Convert.ToInt32 to go from a string to an integer.

You can use Convert.ToString to go from a integer to a String.

Both support both Hex & Binary, as well as octal & decimal (2, 8, 10, or 16
from base)

Dim s As String = "fab4"
Dim i As Integer = Convert.ToInt32(s, 16)

Dim s2 As String = Convert.ToString(i, 2)
Dim i2 As Integer = Convert.ToInt32(s2, 2)

Hope this helps
Jay

MrKrich said:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has
function to do that? I only found Hex function that convert normal integer
to Hexadecimal.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?TXJLcmljaA==?= said:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net
has function to do that? I only found Hex function that convert normal
integer to Hexadecimal.

In addition to Jay's reply, you can use 'Val' to parse a string
containing a number in hexadecimal format.
 
J

Jason L James

Try is code. Is takes the text from TextBox1
and converts it into a string of 1's and 0's that
represent the binary code of the number.

Dim from As Int32
If IsNumeric(TextBox1.Text) Then
from = TextBox1.Text
Dim n As Int16

Do
n = from Mod 2
TextBox2.Text = n & TextBox2.Text
from \= 2
Loop Until from = 0

End If

Hope this helps.

Jason.
 
J

Jay B. Harlow [MVP - Outlook]

Jason,
In addition to trying Convert.ToString as Greg and I suggested.

Consider using <<= or >>= instead of *= & \= by 2.

In VS.NET << is the left shift operator, while >> is the right shift
operator

So:
from \= 2
from >>= 1

Are both the same, while the second may be faster, as bit shifting is
normally faster then division.

Hope this helps
Jay


Jason L James said:
Try is code. Is takes the text from TextBox1
and converts it into a string of 1's and 0's that
represent the binary code of the number.

Dim from As Int32
If IsNumeric(TextBox1.Text) Then
from = TextBox1.Text
Dim n As Int16

Do
n = from Mod 2
TextBox2.Text = n & TextBox2.Text
from \= 2
Loop Until from = 0

End If

Hope this helps.

Jason.
has function to do that? I only found Hex function that convert normal
integer to Hexadecimal.
 
J

Jason L James

Jay/Greg,

Thanks for the feedback. I have been using variations
of the routine I submitted in VB6 and C for a long time.
Your improvements will help the speed and readability
of my code considerably.

The string.convert method has a huge number of
possibilities in my VB.Net code.

Regards,

Jason.
 

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