0
0
CsharpProgramBeginner · 2 min read

C# Program to Convert Decimal to Hexadecimal

Use Convert.ToString(decimalNumber, 16) in C# to convert a decimal number to its hexadecimal string representation.
📋

Examples

Input26
Output1a
Input255
Outputff
Input0
Output0
🧠

How to Think About It

To convert a decimal number to hexadecimal, think of it as changing the number from base 10 to base 16. Each digit in hexadecimal represents values from 0 to 15, so you divide the decimal number by 16 repeatedly and record the remainders. In C#, you can use built-in methods to do this easily without manual division.
📐

Algorithm

1
Get the decimal number input.
2
Use a built-in method to convert the decimal number to a hexadecimal string.
3
Return or print the hexadecimal string.
💻

Code

csharp
using System;

class Program {
    static void Main() {
        int decimalNumber = 255;
        string hexValue = Convert.ToString(decimalNumber, 16);
        Console.WriteLine(hexValue);
    }
}
Output
ff
🔍

Dry Run

Let's trace the decimal number 255 through the code.

1

Input decimal number

decimalNumber = 255

2

Convert to hexadecimal string

hexValue = Convert.ToString(255, 16) => "ff"

3

Print the result

Output: ff

decimalNumberhexValue
255ff
💡

Why This Works

Step 1: Using Convert.ToString method

The Convert.ToString method with base 16 converts the decimal number to a hexadecimal string automatically.

Step 2: Base 16 representation

Hexadecimal uses digits 0-9 and letters a-f to represent values from 0 to 15, so the method returns a string with these characters.

Step 3: Output the result

The program prints the hexadecimal string, which is the converted value of the decimal input.

🔄

Alternative Approaches

Manual division and remainder
csharp
using System;
class Program {
    static void Main() {
        int decimalNumber = 255;
        string hex = "";
        int num = decimalNumber;
        char[] hexDigits = "0123456789abcdef".ToCharArray();
        if (num == 0) hex = "0";
        while (num > 0) {
            int remainder = num % 16;
            hex = hexDigits[remainder] + hex;
            num /= 16;
        }
        Console.WriteLine(hex);
    }
}
This method shows how conversion works step-by-step but is longer and less efficient than built-in methods.
Using ToString method of integer
csharp
using System;
class Program {
    static void Main() {
        int decimalNumber = 255;
        string hexValue = decimalNumber.ToString("x");
        Console.WriteLine(hexValue);
    }
}
This is a simpler and more readable way using <code>ToString("x")</code> format specifier.

Complexity: O(log n) time, O(1) space

Time Complexity

Conversion depends on the number of digits in the hexadecimal representation, which is proportional to log base 16 of the decimal number.

Space Complexity

Only a small fixed amount of extra space is used for the output string and variables, so space is constant.

Which Approach is Fastest?

Using built-in methods like ToString("x") or Convert.ToString is fastest and simplest compared to manual conversion.

ApproachTimeSpaceBest For
Convert.ToString(decimal, 16)O(log n)O(1)Quick and easy conversion
decimal.ToString("x")O(log n)O(1)Most concise and readable
Manual division and remainderO(log n)O(1)Learning how conversion works
💡
Use decimalNumber.ToString("x") for a concise way to convert decimal to hexadecimal.
⚠️
Beginners often forget to specify base 16 or use uppercase format unintentionally, leading to wrong output.