C# 如何获得值的二进制表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12971381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the binary representation of a value
提问by john Gu
Possible Duplicate:
Decimal to binary conversion in c #
可能的重复:
c#中十进制到二进制的转换
I have number such as 3, 432, 1, etc . Where I need to convert these number to set of zero & ones, and then store these bits in an array of integers, but not sure how I can get the bits representation of any integer.
我有 3、432、1 等数字。我需要将这些数字转换为一组零和一,然后将这些位存储在一个整数数组中,但不确定如何获得任何整数的位表示。
回答by Habib
Use Convert.ToString Method (Int32, Int32)
使用Convert.ToString 方法(Int32、Int32)
Converts the value of a 32-bit signed integer to its equivalent string representation in a specified base.
将 32 位有符号整数的值转换为其指定基数中的等效字符串表示形式。
int val = 10;
string binaryNumberString = Convert.ToString(val, 2);
To put them in an int array try:
要将它们放入 int 数组中,请尝试:
int[] arr = new int[binaryNumberString.Length];
int i=0;
foreach (var ch in binaryNumberString)
{
arr[i++] = Convert.ToInt32(ch.ToString());
}
回答by Vinod Vishwanath
You can use the Convert.ToString()method
您可以使用该Convert.ToString()方法
int n = 50;
int b = 2;
string binaryForm = Convert.ToString(n, b);

