I am really stumped on this one. In C# there is a hexadecimal constants representation format as below :
int a = 0xAF2323F5;
is there a binary constants representation format?
Nope, no binary literals in C#. You can of course parse a string in binary format using Convert.ToInt32, but I don't think that would be a great solution.
int bin = Convert.ToInt32( "1010", 2 );
As of C#7 you can represent a binary literal value in code:
private static void BinaryLiteralsFeature()
{
var employeeNumber = 0b00100010; //binary equivalent of whole number 34. Underlying data type defaults to System.Int32
Console.WriteLine(employeeNumber); //prints 34 on console.
long empNumberWithLongBackingType = 0b00100010; //here backing data type is long (System.Int64)
Console.WriteLine(empNumberWithLongBackingType); //prints 34 on console.
int employeeNumber_WithCapitalPrefix = 0B00100010; //0b and 0B prefixes are equivalent.
Console.WriteLine(employeeNumber_WithCapitalPrefix); //prints 34 on console.
}
Further information can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With