Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octal equivalent in C#

Tags:

c#

.net

numbers

In C language octal number can be written by placing 0 before number e.g.

 int i = 012; // Equals 10 in decimal.

I found the equivalent of hexadecimal in C# by placing 0x before number e.g.

 int i = 0xA; // Equals 10 in decimal.

Now my question is: Is there any equivalent of octal number in C# to represent any value as octal?

like image 385
Javed Akram Avatar asked Nov 22 '10 15:11

Javed Akram


People also ask

What is octal equivalent of?

The octal numbers, in the number system, are usually represented by binary numbers when they are grouped in pairs of three. For example, an octal number 128 is expressed as 0010102 in the binary system, where 1 is equivalent to 001 and 2 is equivalent to 010. Octal Number System.

How do you find an octal equivalent?

In case of decimal to octal, we divide the number by 8 and write the remainders in the reverse order to get the equivalent octal number. Decimal Number: All the numbers to the base ten are called decimal numbers. These are the commonly used numbers, which are 0-9.


4 Answers

No, there are no octal number literals in C#.

For strings: Convert.ToInt32("12", 8) returns 10.

like image 58
ulrichb Avatar answered Oct 11 '22 23:10

ulrichb


No there isn't, the language specification (ECMA-334) is quite specific.

4th edition, page 72

9.4.4.2 Integer literals

Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two possible forms: decimal and hexadecimal.

No octal form.

like image 36
Julien Roncaglia Avatar answered Oct 12 '22 00:10

Julien Roncaglia


No, there are no octal literals in C#.

If necessary, you could pass a string and a base to Convert.ToInt32, but it's obviously nowhere near as nice as a literal:

int i = Convert.ToInt32("12", 8);
like image 22
LukeH Avatar answered Oct 11 '22 23:10

LukeH


No, there are no octal numbers in C#.

Use public static int ToInt32(string value, int fromBase);

fromBase
Type: System.Int32
The base of the number in value, which must be 2, 8, 10, or 16.

MSDN

like image 9
abatishchev Avatar answered Oct 12 '22 00:10

abatishchev