×

C# Crash Course

Course Home C# Introduction and Setup C# Variables and Data Types C# Console Input and Output C# Operators and Expressions C# Conditional Statements - If and Else C# Switch Statements C# While and Do-While Loops C# For Loops C# Nested Loops C# Methods - Part 1 (Basics) C# Methods - Part 2 (ref, out, and Recursion) C# Arrays C# Number Systems (Binary and Hexadecimal) C# Exception Handling (Try-Catch) C# Random Numbers C# String Methods and Manipulation C# Course Summary and Best Practices


C# Number Systems (Binary and Hexadecimal)

Understanding Number Systems

Different number systems use different bases to represent numbers:

System Base Digits Example
Decimal 10 0-9 125
Binary 2 0-1 1111101
Hexadecimal 16 0-9, A-F 7D

Binary (Base 2)

Binary uses only 0 and 1. Each position represents a power of 2.

Binary to Decimal Conversion

Binary: 1 0 1 1
           
        8 4 2 1  (powers of 2)
           
        1×8 + 0×4 + 1×2 + 1×1 = 8 + 0 + 2 + 1 = 11 (decimal)

More Examples

Binary 1111 = 1×8 + 1×4 + 1×2 + 1×1 = 15
Binary 1000 = 1×8 + 0×4 + 0×2 + 0×1 = 8
Binary 0101 = 0×8 + 1×4 + 0×2 + 1×1 = 5

4-Bit Binary Values (Nibble)

Binary Decimal Binary Decimal
0000 0 1000 8
0001 1 1001 9
0010 2 1010 10
0011 3 1011 11
0100 4 1100 12
0101 5 1101 13
0110 6 1110 14
0111 7 1111 15

Hexadecimal (Base 16)

Hexadecimal uses digits 0-9 and letters A-F (A=10, B=11, C=12, D=13, E=14, F=15).

Hexadecimal Values

Hex Decimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

Hexadecimal to Decimal Conversion

Hex: 2 A F
       
     256 16 1  (powers of 16)
          
     2×256 + 10×16 + 15×1 = 512 + 160 + 15 = 687 (decimal)

Binary to Hexadecimal Conversion

Key Relationship: Each hexadecimal digit represents exactly 4 binary digits (bits).

Method: Group by 4

Binary: 1010 1111 0011
                 
        10   15    3
                 
         A    F    3

Answer: AF3 (hexadecimal)

Example from Practical Test 3

Convert 12-bit binary to hexadecimal:

Binary: 1011 0110 1001
                 
        11    6    9
                 
         B    6    9

Answer: B69

Practical Implementation (From Practical Test 3)

Binary to Hexadecimal Converter

using System;

namespace BinaryToHex
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a 12-bit binary number: ");

            // Read 12 characters
            int i4Bit_G1_1 = Console.Read() - 48;
            int i4Bit_G1_2 = Console.Read() - 48;
            int i4Bit_G1_3 = Console.Read() - 48;
            int i4Bit_G1_4 = Console.Read() - 48;

            int i4Bit_G2_1 = Console.Read() - 48;
            int i4Bit_G2_2 = Console.Read() - 48;
            int i4Bit_G2_3 = Console.Read() - 48;
            int i4Bit_G2_4 = Console.Read() - 48;

            int i4Bit_G3_1 = Console.Read() - 48;
            int i4Bit_G3_2 = Console.Read() - 48;
            int i4Bit_G3_3 = Console.Read() - 48;
            int i4Bit_G3_4 = Console.Read() - 48;

            // Convert each group of 4 bits to decimal
            int iHex_1 = i4Bit_G1_1 * 8 + i4Bit_G1_2 * 4 + i4Bit_G1_3 * 2 + i4Bit_G1_4;
            int iHex_2 = i4Bit_G2_1 * 8 + i4Bit_G2_2 * 4 + i4Bit_G2_3 * 2 + i4Bit_G2_4;
            int iHex_3 = i4Bit_G3_1 * 8 + i4Bit_G3_2 * 4 + i4Bit_G3_3 * 2 + i4Bit_G3_4;

            // Convert to hex characters
            char chHex_1 = ' ', chHex_2 = ' ', chHex_3 = ' ';

            // First digit
            if (iHex_1 >= 10 && iHex_1 <= 15)
            {
                switch (iHex_1)
                {
                    case 10: chHex_1 = 'A'; break;
                    case 11: chHex_1 = 'B'; break;
                    case 12: chHex_1 = 'C'; break;
                    case 13: chHex_1 = 'D'; break;
                    case 14: chHex_1 = 'E'; break;
                    case 15: chHex_1 = 'F'; break;
                }
            }
            else
            {
                chHex_1 = (char)(iHex_1 + '0');  // Convert 0-9 to char
            }

            // Second digit
            if (iHex_2 >= 10 && iHex_2 <= 15)
            {
                switch (iHex_2)
                {
                    case 10: chHex_2 = 'A'; break;
                    case 11: chHex_2 = 'B'; break;
                    case 12: chHex_2 = 'C'; break;
                    case 13: chHex_2 = 'D'; break;
                    case 14: chHex_2 = 'E'; break;
                    case 15: chHex_2 = 'F'; break;
                }
            }
            else
            {
                chHex_2 = (char)(iHex_2 + '0');
            }

            // Third digit
            if (iHex_3 >= 10 && iHex_3 <= 15)
            {
                switch (iHex_3)
                {
                    case 10: chHex_3 = 'A'; break;
                    case 11: chHex_3 = 'B'; break;
                    case 12: chHex_3 = 'C'; break;
                    case 13: chHex_3 = 'D'; break;
                    case 14: chHex_3 = 'E'; break;
                    case 15: chHex_3 = 'F'; break;
                }
            }
            else
            {
                chHex_3 = (char)(iHex_3 + '0');
            }

            string sMsg = chHex_1.ToString() + chHex_2.ToString() + chHex_3.ToString();

            Console.WriteLine($"\n\nThe Hexadecimal number is {sMsg}");

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

Hexadecimal to Decimal Converter (From Worksheet 3)

using System;

namespace HexToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a 3-digit hexadecimal number: ");

            char chHex1 = Convert.ToChar(Console.Read());
            char chHex2 = Convert.ToChar(Console.Read());
            char chHex3 = Convert.ToChar(Console.Read());

            int iHex1 = 0, iHex2 = 0, iHex3 = 0;

            // Convert first digit
            if (chHex1 >= 'A' && chHex1 <= 'F')
            {
                switch (chHex1)
                {
                    case 'A': iHex1 = 10; break;
                    case 'B': iHex1 = 11; break;
                    case 'C': iHex1 = 12; break;
                    case 'D': iHex1 = 13; break;
                    case 'E': iHex1 = 14; break;
                    case 'F': iHex1 = 15; break;
                }
            }
            else
            {
                iHex1 = chHex1 - 48;  // Convert '0'-'9' to 0-9
            }

            // Convert second digit
            if (chHex2 >= 'A' && chHex2 <= 'F')
            {
                switch (chHex2)
                {
                    case 'A': iHex2 = 10; break;
                    case 'B': iHex2 = 11; break;
                    case 'C': iHex2 = 12; break;
                    case 'D': iHex2 = 13; break;
                    case 'E': iHex2 = 14; break;
                    case 'F': iHex2 = 15; break;
                }
            }
            else
            {
                iHex2 = chHex2 - 48;
            }

            // Convert third digit
            if (chHex3 >= 'A' && chHex3 <= 'F')
            {
                switch (chHex3)
                {
                    case 'A': iHex3 = 10; break;
                    case 'B': iHex3 = 11; break;
                    case 'C': iHex3 = 12; break;
                    case 'D': iHex3 = 13; break;
                    case 'E': iHex3 = 14; break;
                    case 'F': iHex3 = 15; break;
                }
            }
            else
            {
                iHex3 = chHex3 - 48;
            }

            // Calculate decimal value
            int iDecimalNumber = (iHex1 * 16 * 16) + (iHex2 * 16) + iHex3;

            Console.WriteLine($"\n\nThe decimal number is: {iDecimalNumber}");

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

ASCII Values

ASCII (American Standard Code for Information Interchange) assigns numbers to characters.

Important ASCII Values

Character ASCII Value
'0' 48
'1' 49
'2' 50
... ...
'9' 57
'A' 65
'B' 66
... ...
'Z' 90
'a' 97
'b' 98
... ...
'z' 122

Converting Characters to Numbers

// Character '5' to integer 5
char ch = '5';
int iValue = ch - 48;  // 53 - 48 = 5
// Or
int iValue2 = ch - '0';  // Same thing

// Character 'A' to integer 10
char chHex = 'A';
int iHexValue = chHex - 'A' + 10;  // 65 - 65 + 10 = 10

// Integer 5 to character '5'
int iNum = 5;
char chDigit = (char)(iNum + '0');  // 5 + 48 = 53 = '5'

Console.Read() Explanation

Console.Read() returns the ASCII value of a character:

Console.Write("Enter a digit: ");
int iAscii = Console.Read();  // If user enters '3', returns 51

// Convert to actual number
int iNumber = iAscii - 48;  // 51 - 48 = 3

Manual Conversion Methods

Decimal to Binary (Manual)

int iDecimal = 13;
string sBinary = "";

while (iDecimal > 0)
{
    int iRemainder = iDecimal % 2;
    sBinary = iRemainder + sBinary;  // Add to front
    iDecimal = iDecimal / 2;
}

Console.WriteLine(sBinary);  // "1101"

Decimal to Hexadecimal (Manual)

int iDecimal = 255;
string sHex = "";

while (iDecimal > 0)
{
    int iRemainder = iDecimal % 16;

    char chDigit;
    if (iRemainder < 10)
    {
        chDigit = (char)(iRemainder + '0');
    }
    else
    {
        chDigit = (char)(iRemainder - 10 + 'A');
    }

    sHex = chDigit + sHex;
    iDecimal = iDecimal / 16;
}

Console.WriteLine(sHex);  // "FF"

Using Built-in Convert Methods

Note: Your course doesn't allow these methods in exams, but they're useful to know:

// Decimal to Binary
string sBinary = Convert.ToString(13, 2);  // "1101"

// Decimal to Hexadecimal
string sHex = Convert.ToString(255, 16);   // "ff"

// Binary to Decimal
int iDecimal = Convert.ToInt32("1101", 2);  // 13

// Hexadecimal to Decimal
int iDecimal2 = Convert.ToInt32("FF", 16);  // 255

Practice Conversions

Binary to Decimal Practice

1. 1010 = ?
2. 1111 = ?
3. 10101 = ?
4. 11001 = ?
5. 1000000 = ?

Answers:
1. 10
2. 15
3. 21
4. 25
5. 64

Hexadecimal to Decimal Practice

1. 1F = ?
2. A5 = ?
3. FF = ?
4. 100 = ?
5. 2A3 = ?

Answers:
1. 31
2. 165
3. 255
4. 256
5. 675

Binary to Hexadecimal Practice

1. 1111 0000 = ?
2. 1010 1011 = ?
3. 0011 1100 = ?
4. 1111 1111 = ?

Answers:
1. F0
2. AB
3. 3C
4. FF

Complete Example: Number System Converter

using System;

namespace NumberSystemConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Number System Converter ===");
            Console.WriteLine("1. Binary to Decimal");
            Console.WriteLine("2. Decimal to Binary");
            Console.WriteLine("3. Hexadecimal to Decimal");
            Console.WriteLine("4. Binary to Hexadecimal");
            Console.Write("\nChoose option: ");

            int iChoice = int.Parse(Console.ReadLine());

            switch (iChoice)
            {
                case 1:
                    BinaryToDecimal();
                    break;
                case 2:
                    DecimalToBinary();
                    break;
                case 3:
                    HexadecimalToDecimal();
                    break;
                case 4:
                    BinaryToHexadecimal();
                    break;
                default:
                    Console.WriteLine("Invalid option");
                    break;
            }

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }

        static void BinaryToDecimal()
        {
            Console.Write("Enter binary number: ");
            string sBinary = Console.ReadLine();

            int iDecimal = 0;
            int iPower = 0;

            for (int i = sBinary.Length - 1; i >= 0; i--)
            {
                if (sBinary[i] == '1')
                {
                    iDecimal += (int)Math.Pow(2, iPower);
                }
                iPower++;
            }

            Console.WriteLine($"Decimal: {iDecimal}");
        }

        static void DecimalToBinary()
        {
            Console.Write("Enter decimal number: ");
            int iDecimal = int.Parse(Console.ReadLine());

            string sBinary = "";

            while (iDecimal > 0)
            {
                sBinary = (iDecimal % 2) + sBinary;
                iDecimal = iDecimal / 2;
            }

            Console.WriteLine($"Binary: {sBinary}");
        }

        static void HexadecimalToDecimal()
        {
            Console.Write("Enter hexadecimal number: ");
            string sHex = Console.ReadLine().ToUpper();

            int iDecimal = 0;
            int iPower = 0;

            for (int i = sHex.Length - 1; i >= 0; i--)
            {
                int iValue;

                if (sHex[i] >= '0' && sHex[i] <= '9')
                {
                    iValue = sHex[i] - '0';
                }
                else
                {
                    iValue = sHex[i] - 'A' + 10;
                }

                iDecimal += iValue * (int)Math.Pow(16, iPower);
                iPower++;
            }

            Console.WriteLine($"Decimal: {iDecimal}");
        }

        static void BinaryToHexadecimal()
        {
            Console.Write("Enter binary number (multiple of 4 bits): ");
            string sBinary = Console.ReadLine();

            string sHex = "";

            // Process 4 bits at a time
            for (int i = 0; i < sBinary.Length; i += 4)
            {
                string s4Bits = sBinary.Substring(i, 4);

                // Convert 4 bits to decimal
                int iValue = 0;
                for (int j = 0; j < 4; j++)
                {
                    if (s4Bits[j] == '1')
                    {
                        iValue += (int)Math.Pow(2, 3 - j);
                    }
                }

                // Convert to hex character
                if (iValue < 10)
                {
                    sHex += (char)(iValue + '0');
                }
                else
                {
                    sHex += (char)(iValue - 10 + 'A');
                }
            }

            Console.WriteLine($"Hexadecimal: {sHex}");
        }
    }
}

Practice Exercises

Exercise 1: 8-bit Binary Converter

Write a program that converts an 8-bit binary number to decimal.

Exercise 2: Hexadecimal Validator

Write a program that checks if a string is a valid hexadecimal number.

Exercise 3: Binary Addition

Write a program that adds two 4-bit binary numbers.

Exercise 4: Color Code Converter

Convert RGB color codes (0-255) to hexadecimal format (#RRGGBB).

Key Takeaways

✅ Binary uses base 2 (digits 0-1) ✅ Hexadecimal uses base 16 (digits 0-9, A-F) ✅ Each hex digit = 4 binary bits ✅ ASCII value of '0' is 48, 'A' is 65 ✅ Subtract 48 to convert char digit to int ✅ Use Console.Read() to read individual characters ✅ Cannot use Convert methods in exams ✅ Group binary by 4 to convert to hex


Next Topic: C# Try-Catch (Exception Handling)