I'd recommend you see if this CP article might meet all your needs: "Number base conversion class in C# by MarkGwilliam | 23 Dec 2006"[^
], and then consider the information presented below.
However, the article linked to above does not deal with "exess 3 and to graycode."
There are no native representations for binary (base 2) or octal (base 8) integers in C#. The only possible integer "literals" are decimal and hexadecimal.
You can convert an integer which you intend
to be octal or binary, to its equivalent integer value in bases 2, 8,10,16 by using the Convert.ToInt32(string value, int base)
method which requires the first argument (the integer to be converted) to be in string form: the second argument specifies the base.
int binInt1 = 11110;
octInt1 = 64;
// then execute this code:
int base8int = Convert.ToInt32(octInt1.ToString(), 8);
int base2int = Convert.ToInt32(binInt1.ToString(), 2);
And examine the results.
Of course once you have the actual integer value, converting to hexadecimal is trivial:
int base2AsHex = Convert.ToInt32(base2int.ToString(), 16);
But, you'll note that in all these examples you are "paying the price" for string conversion ... unless in your application you are already expressing octals and binaries as string.
Hence solutions, like the one cited at the top of this response, that try to speed-up conversions via code, and as found as in the answer by CPallini above, and, also, here:[^
I find it strange that C# did not come with native built-in numeric base conversions that did not require string conversion or input.