Work with Non–Base 10 Number Systems

Problem

You want to convert a base 10 number to a hexadecimal, octal, or binary number (or vice versa).

Solution

Use the overloaded Convert.ToString and Convert.ToInt32 shared methods that accept a number indicating the base.

Discussion

Although you can’t work directly with non–base 10 numbers in Visual Basic .NET, you can easily convert base 10 values into a string representation that uses a base 2 (binary), base 8 (octal), base 10, or base 16 (hexadecimal). To do so, you use the overloaded Convert.ToString method that accepts two parameters: the base 10 number and the base that should be used for the converted number (which must be 2, 8, 10, or 16).

Dim Number As Integer = 3023 Console.WriteLine("Binary: " & Convert.ToString(Number, ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.