468,554 Members | 1,942 Online

# Function that convert Hexadecimal to Binary?

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.
Nov 20 '05 #1
6 41990
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" <Mr*****@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
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
Nov 20 '05 #2
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.
On Sun, 18 Jul 2004 08:21:06 -0700, "=?Utf-8?B?TXJLcmljaA==?="
<Mr*****@discussions.microsoft.com> wrote:
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.

Nov 20 '05 #3
* "=?Utf-8?B?TXJLcmljaA==?=" <Mr*****@discussions.microsoft.com> scripsit:
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

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #4
Jason, try this shortcut

TextBox2.Text=Convert.ToString(from, 2)

"Jason L James" <ja***@no-spam.dive-master.org> wrote in message
news:40***************@news.newnet.co.uk...
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.
On Sun, 18 Jul 2004 08:21:06 -0700, "=?Utf-8?B?TXJLcmljaA==?="
<Mr*****@discussions.microsoft.com> wrote:
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

Nov 20 '05 #5
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" <ja***@no-spam.dive-master.org> wrote in message
news:40***************@news.newnet.co.uk... 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.
On Sun, 18 Jul 2004 08:21:06 -0700, "=?Utf-8?B?TXJLcmljaA==?="
<Mr*****@discussions.microsoft.com> wrote:
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

Nov 20 '05 #6
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.

On Sun, 18 Jul 2004 11:59:05 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
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" <ja***@no-spam.dive-master.org> wrote in message
news:40***************@news.newnet.co.uk...
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.
On Sun, 18 Jul 2004 08:21:06 -0700, "=?Utf-8?B?TXJLcmljaA==?="
<Mr*****@discussions.microsoft.com> wrote:
>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