4

可能重复:
c#中的十进制到二进制转换

我有 3、432、1 等数字。我需要将这些数字转换为零和一的集合,然后将这些位存储在整数数组中,但不确定如何获得任何整数的位表示。

4

2 回答 2

17

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());
}
于 2012-10-19T09:37:39.797 回答
6

You can use the Convert.ToString() method

int n = 50;
int b = 2;

string binaryForm = Convert.ToString(n, b);
于 2012-10-19T09:38:03.210 回答