可能重复:
c#中的十进制到二进制转换
我有 3、432、1 等数字。我需要将这些数字转换为零和一的集合,然后将这些位存储在整数数组中,但不确定如何获得任何整数的位表示。
Use Convert.ToString Method (Int32, Int32)
Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base.
int val = 10;
string binaryNumberString = Convert.ToString(val, 2);
To put them in an int array try:
int[] arr = new int[binaryNumberString.Length];
int i=0;
foreach (var ch in binaryNumberString)
{
arr[i++] = Convert.ToInt32(ch.ToString());
}
You can use the Convert.ToString()
method
int n = 50;
int b = 2;
string binaryForm = Convert.ToString(n, b);