0

I followed how to write octal literal but it seems not working:

int b = 0145;
Console.WriteLine(b); // prints 145

I'm trying to convert from octal to decimal, I tried hex and binary literals and it's working fine but not octal. What's the problem?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
codelover1
  • 135
  • 2
  • 7
  • Use Console.WriteLine(Convert.ToString(b, 8)), where the 2nd parameter to Convert.ToString is the numeric base (2,8,10,16, etc). – Cobster Aug 22 '22 at 17:31
  • What is an "octal literal"? `0145` is an Int32 literal, which you are assigning to an Int32, and printing it using the Int32 parameter. – gunr2171 Aug 22 '22 at 17:38
  • Ouch. Yeah, always look for the spec. Good SO answers are generally also pretty safe to rely on especially if they cite specs. You sure you weren't on a C/C++ site? – General Grievance Aug 22 '22 at 17:44
  • @GeneralGrievance Nope, I were on geeksforgeeks they had the same answer and they didn't write that it's not working, I dont know why tho – codelover1 Aug 22 '22 at 17:46
  • Oh, [was this the page](https://www.geeksforgeeks.org/c-sharp-literals/)? Yeah, they're wrong for sure. In general, for language references, you'll want to try to stick with more reputable sources. For C#, use MS's own docs. For web (JS/HTML/CSS), use MDN, or if not there, the official W3 spec (*not* W3Schools). Good, reliable SO posts tend to point to those sites as well. – General Grievance Aug 22 '22 at 19:02
  • @GeneralGrievance yes that page – codelover1 Aug 22 '22 at 19:56

0 Answers0