Use the "Convert" class.
string binStr = "1100110001"; long number = Convert.ToInt64(binStr, 2); Console.WriteLine(binStr + " in binary : " + number + " in decimal");also from C# version 7 it is possible to declare numeric values in terms of binary literals:
int binaryLiteral = 0b0010_0110_0000_0011; int binaryLiteral2 = 0b0010011000000011; Debug.Assert(binaryLiteral == binaryLiteral2); Console.WriteLine("0b0010_0110_0000_0011 in binary : " + binaryLiteral + " in decimal");Finally to convert a number to a binary string
int number = 9731; string binary = Convert.ToString(number, 2); Console.WriteLine(number + " in binary : " + binary);
No comments:
Post a Comment