C# 将字符串转换为二进制零和一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16659787/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:36:04  来源:igfitidea点击:

Convert string to binary zeros and ones

c#stringbinary

提问by Kyara Programmer

I want to convert string to binary and I tried this code

我想将字符串转换为二进制,我试过这段代码

byte[] arr = System.Text.Encoding.ASCII.GetBytes(aa[i]);

and this

和这个

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] arr= encoding.GetBytes(aa[i]);

but it returned numbers not binary say If I write 'm' it convert it to "0109" and I want to convert to zeros & Ones only Thanks in advance

但它返回的数字不是二进制说如果我写 'm' 它将它转换为“0109”,我只想转换为 0 和 1 提前致谢

采纳答案by mitja.gti

Here is an example:

下面是一个例子:

foreach (char c in "Test")
    Console.WriteLine(Convert.ToString(c, 2).PadLeft(8, '0'));

回答by cat_minhv0

You could use : Convert.ToByte(string value, int fromBase)

您可以使用:Convert.ToByte(string value, int fromBase)

According to MSDN :

根据 MSDN :

fromBase Type: System.Int32 The base of the number in value, which must be 2, 8, 10, or 16.

fromBase 类型:System.Int32 value 中数字的基数,必须是 2、8、10 或 16。

For more detail, see this link : Convert.ToByte()

有关更多详细信息,请参阅此链接:Convert.ToByte()

回答by LemonCool

I'm not sure I completely understand your question, but if you want to convert text to binary this is how it is done:

我不确定我是否完全理解你的问题,但如果你想将文本转换为二进制,这是如何完成的:

public byte[] ToBinary(string stringPassed)
{
   System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
   return encoding.GetBytes(stringPassed);
}

You need to pass the whole string, not the characters in the string.

您需要传递整个字符串,而不是字符串中的字符。

回答by weston

So you can get to bytes, now you want to output 0s and 1s.

所以你可以得到字节,现在你想输出 0 和 1。

For a single byte b

对于单 byte b

var sb = new StringBuilder();
for(var i=7;i>=0;i--)
{
  sb.Append((b & (1<<i))==0?'0':'1');
}

回答by Nicholas Carey

Something like this ought to do you;

你应该这样做;

static string bytes2bin( byte[] bytes )
{
  StringBuilder buffer = new StringBuilder(bytes.Length*8) ;
  foreach ( byte b in bytes )
  {
    buffer.Append(lookup[b]) ;
  }
  string binary = buffer.ToString() ;
  return binary ;
}

static readonly string[] lookup = InitLookup() ;
private static string[] InitLookup()
{
  string[] instance = new string[1+byte.MaxValue] ;
  StringBuilder buffer = new StringBuilder("00000000") ;
  for ( int i = 0 ; i < instance.Length ; ++i )
  {

    buffer[0] = (char)( '0' + ((i>>7)&1) ) ;
    buffer[1] = (char)( '0' + ((i>>6)&1) ) ;
    buffer[2] = (char)( '0' + ((i>>5)&1) ) ;
    buffer[3] = (char)( '0' + ((i>>4)&1) ) ;
    buffer[4] = (char)( '0' + ((i>>3)&1) ) ;
    buffer[5] = (char)( '0' + ((i>>2)&1) ) ;
    buffer[6] = (char)( '0' + ((i>>1)&1) ) ;
    buffer[7] = (char)( '0' + ((i>>0)&1) ) ;

    instance[i] = buffer.ToString() ;
  }
  return instance ;
}