Represent Int32 as a Octal String in C#

To represent Int32 as an octal string in C#, use the Convert.ToString() method with base 8 as the second parameter. An Int32 represents a 32-bit signed integer that can hold values from -2,147,483,648 to 2,147,483,647.

Syntax

Following is the syntax for converting an Int32 to octal string −

Convert.ToString(intValue, 8)

Parameters

  • intValue − The integer value to convert.

  • 8 − The base for octal representation.

Return Value

The method returns a string representing the integer in octal format (base 8).

Decimal to Octal Conversion Decimal 99 ? Octal 143 Base 10 ? Base 8 99 = 1×64 + 4×8 + 3×1

Example

Converting Positive Integer to Octal

using System;

class Demo {
    static void Main() {
        int val = 99;
        Console.WriteLine("Integer: " + val);
        Console.WriteLine("Octal String: " + Convert.ToString(val, 8));
    }
}

The output of the above code is −

Integer: 99
Octal String: 143

Converting Multiple Values to Octal

using System;

class Program {
    static void Main() {
        int[] numbers = { 8, 64, 255, 1024 };
        
        Console.WriteLine("Decimal to Octal Conversion:");
        foreach (int num in numbers) {
            string octalString = Convert.ToString(num, 8);
            Console.WriteLine($"{num} (decimal) = {octalString} (octal)");
        }
    }
}

The output of the above code is −

Decimal to Octal Conversion:
8 (decimal) = 10 (octal)
64 (decimal) = 100 (octal)
255 (decimal) = 377 (octal)
1024 (decimal) = 2000 (octal)

Handling Negative Numbers

using System;

class Program {
    static void Main() {
        int positive = 50;
        int negative = -50;
        
        Console.WriteLine("Positive number:");
        Console.WriteLine($"{positive} = {Convert.ToString(positive, 8)}");
        
        Console.WriteLine("\nNegative number:");
        Console.WriteLine($"{negative} = {Convert.ToString(negative, 8)}");
    }
}

The output of the above code is −

Positive number:
50 = 62

Negative number:
-50 = 37777777716

How It Works

The octal number system uses base 8, with digits 0-7. Each position represents a power of 8. For example, the decimal number 99 converts to octal 143 because:

  • 99 ÷ 8 = 12 remainder 3 (rightmost digit)

  • 12 ÷ 8 = 1 remainder 4 (middle digit)

  • 1 ÷ 8 = 0 remainder 1 (leftmost digit)

For negative numbers, C# uses two's complement representation, resulting in longer octal strings with leading 3s.

Conclusion

Use Convert.ToString(intValue, 8) to represent any Int32 value as an octal string in C#. This method handles both positive and negative integers, converting them from decimal (base 10) to octal (base 8) representation efficiently.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements