Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data Conversion (binary, hex, dec., etc.)

41 views
Skip to first unread message

Amjad

unread,
Feb 18, 2005, 4:51:05 PM2/18/05
to
Hi,

Are there any built-in methods in VB.NET that convert one number in one
format to its equivalent in another format?

For example I want to convert the number 162 (decimal) to 10100010 (binary)
or to A2 (hexadecimal) and vice versa.

Thanks.

Herfried K. Wagner [MVP]

unread,
Feb 18, 2005, 5:00:37 PM2/18/05
to
"Amjad" <Am...@discussions.microsoft.com> schrieb:

VB provides the functions 'Hex' and 'Oct'. You can use 'CLng' to parse a
string containing a number in dexadecimal or octal format:

\\\
Dim s As String = Hex(222)
MsgBox(s)
Dim i As Integer = CInt("&H" & s)
MsgBox(CStr(i))
///

The same using the .NET FCL:

\\\
Dim s As String = Convert.ToString(222, 16)
MsgBox(s)
Dim i As Integer = Convert.ToInt32(s, 16)
MsgBox(CStr(i))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Amjad

unread,
Feb 21, 2005, 11:19:05 AM2/21/05
to
Thanks.

Is there any built-in methods that convert decimal to BINARY?
Or do I have to write this conversion function myself?

Amjad

Jay B. Harlow [MVP - Outlook]

unread,
Feb 22, 2005, 11:32:46 AM2/22/05
to
Amjad,
As Herfried stated, you can use the Convert class to do this.

An integer is always stored as an Integer. When you are talking decimal &
binary you are "really" talking about string representations of those
numbers

The second parameter to Convert.ToString & Convert.ToInt32 is the base you
desire.

So to convert an Integer to Binary you would use Convert.ToString(222, 2),
to convert the same integer to Decimal you can use Convert.ToString(222,
10). Of course Decimal is the normal string reprentation of numbers so you
can just as easily use Convert.ToString(222) or 222.ToString().

Hope this helps
Jay


"Amjad" <Am...@discussions.microsoft.com> wrote in message
news:C5CA57EF-8B7E-4594...@microsoft.com...

0 new messages