Tuesday, January 24, 2012

3.2.4. Changing the base

An alternative use for "ToString" is to change the base for a number. For example, we usually work with decimal numbers (base 10), but in computing are also very common base 2 (binary system) and base 16 (hexadecimal system). We can convert a number to binary or hexadecimal (or octal base, less frequently) using Convert.ToString and indicating the desired base, as in this example:

/*---------------------------*/
/*  C# Example #28b:         */
/*  example28b.cs            */
/*                           */
/*  Hexadecimal and binary   */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example28b
{
    public static void Main()
    {
        int number = 247;
       
        Console.WriteLine( Convert.ToString(number, 16) );
        Console.WriteLine( Convert.ToString(number, 2) );
    }
}

The result is:

f7
11110111

(If you want to learn more about the hexadecimal system, you can have a look at the appendices at the end of this text)

Suggested exercises:

Ø       Create a program that asks the user for numbers (base 10) and displays its equivalent in binary and hexadecimal. It should be repeated until the user enters the number 0.
Ø       Write a program that asks the user for the amount of red (eg, 255), green (eg, 160) and blue (for example, 0) which has a color, and show that RGB color in hexadecimal notation (for example, FFA000)
Ø       Create a program to display the hexadecimal numbers from 0 to 255 (decimal), in 16 rows with 16 numbers each (first row will contain the numbers 0 to 15 -decimal-, second will have 16 to 31 -decimal-, and so on).


To convert from hexadecimal or binary to decimal, we can use Convert.ToInt32, as shown in the following example. Note that a hexadecimal constant value can be preceded with "0x", as in "int n1 = 0x12a3;" but a value preceded with "0" is not considered to be octal but decimal, opposite to the behavior in C and C++ languages:

/*---------------------------*/
/*  C# Example #28c:         */
/*  example28c.cs            */
/*                           */
/*  Hexadecimal and binary   */
/* (2)                       */
/*                           */
/*  Intro to C#,             */
/*    Nacho Cabanes          */
/*---------------------------*/

using System;

public class Example28c
{
    public static void Main()
    {
        int n1 = 0x12a3;
        int n2 = Convert.ToInt32("12a4", 16);
       
        int n3 = 0123; // Not octal, opposite to C and C++
        int n4 = Convert.ToInt32("124", 8);
       
        int n5 = Convert.ToInt32("11001001", 2);
       
        double d1 = 5.7;
        float f1 = 5.7f;
       
        Console.WriteLine( "{0} {1} {2} {3} {4}",
            n1, n2, n3, n4, n5);
        Console.WriteLine( "{0} {1}",
            d1, f1);
    }
}

The result is:

4771 4772 123 84 201
5,7 5,7


Note: The notation "float f1 = 5.7f;" is used to specify that it is a single precision real number (a "float"), because if we use "float f1 = 5.7;", it would be considered a double precision number and we might get a compile error, telling us that the compiler can not convert from 'double' to 'float' without loss of accuracy. When we add the "f" at the end of the number, we are saying "I want this number to be taken as a float, I know there will be a loss of precision but it is acceptable to me."

Suggested exercises:

Ø       Create a program that asks the user for binary numbers and displays their equivalent in hexadecimal and decimal. It should be repeated until the user enters the word "end."

No comments:

Post a Comment