Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Represent Int64 as a Octal string in C#
To represent Int64 as an octal string in C#, use the Convert.ToString() method and set the base as the second parameter to 8 for octal conversion.
Int64 represents a 64-bit signed integer that can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Syntax
Following is the syntax for converting Int64 to octal string −
Convert.ToString(longValue, 8)
Parameters
longValue − The Int64 value to convert
8 − The base for octal number system
Return Value
Returns a string representation of the Int64 value in octal format.
Example
using System;
class Demo {
static void Main() {
long val = 986766;
Console.WriteLine("Long: " + val);
Console.WriteLine("Octal String: " + Convert.ToString(val, 8));
}
}
The output of the above code is −
Long: 986766 Octal String: 3607216
Using Multiple Int64 Values
Example
using System;
class Program {
static void Main() {
long[] values = { 64, 255, 1024, 8192, 986766 };
Console.WriteLine("Decimal\t\tOctal");
Console.WriteLine("-------\t\t-----");
foreach (long value in values) {
string octalString = Convert.ToString(value, 8);
Console.WriteLine($"{value}\t\t{octalString}");
}
}
}
The output of the above code is −
Decimal Octal ------- ----- 64 100 255 377 1024 2000 8192 20000 986766 3607216
Converting Negative Int64 Values
Example
using System;
class Program {
static void Main() {
long negativeValue = -986766;
long positiveValue = 986766;
Console.WriteLine("Positive: " + positiveValue + " -> Octal: " + Convert.ToString(positiveValue, 8));
Console.WriteLine("Negative: " + negativeValue + " -> Octal: " + Convert.ToString(negativeValue, 8));
}
}
The output of the above code is −
Positive: 986766 -> Octal: 3607216 Negative: -986766 -> Octal: 1777777777777777774170562
Conclusion
The Convert.ToString() method with base 8 efficiently converts Int64 values to their octal string representation. For negative numbers, the method returns the two's complement representation in octal format, which appears as a long string of leading 7s.
