Javed Akram
Javed Akram

Reputation: 15344

Octal equivalent in C#

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?

Upvotes: 62

Views: 36449

Answers (5)

Julien Roncaglia
Julien Roncaglia

Reputation: 17837

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.

Upvotes: 38

abatishchev
abatishchev

Reputation: 100258

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

Upvotes: 9

Mike Dour
Mike Dour

Reputation: 3766

You can't use literals, but you can parse an octal number: Convert.ToInt32("12", 8).

Upvotes: 3

LukeH
LukeH

Reputation: 269358

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);

Upvotes: 24

ulrichb
ulrichb

Reputation: 20054

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

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

Upvotes: 64

Related Questions