我怎么能获得在C#中的字符串中的位? [英] How could I get the bits from a string in c#?
本文介绍了我怎么能获得在C#中的字符串中的位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有以下字符串蓝箱,我怎么能得到弥补字符串在C#中,什么数据类型我把它存储在位。
If I had the following string "Blue Box", how could I get the bits that make up the string in c# and what datatype would I store it in.
如果我这样做只是字母O,我得到111字节和111位。难道斩去0的,如果我做OO,我得到111字节数组中的每个0,但对位,我得到的价值28527.为什么?
If I do just the letter "o", I get 111 as the bytes and 111 as the bits. Is it chopping off the 0's and if I do "oo", I get 111 for each o in the byte array, but for the bits, I get the value 28527. Why?
推荐答案
如果你想在一个字符串格式位,你可以使用此功能:
If you want the bits in a string format, you could use this function:
public string GetBits(string input)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in Encoding.Unicode.GetBytes(input))
{
sb.Append(Convert.ToString(b, 2));
}
return sb.ToString();
}
如果您使用蓝箱例如,你可以:
If you use your "Blue Box" example you get:
string bitString = GetBits("Blue Box");
// bitString == "100001001101100011101010110010101000000100001001101111011110000"
这篇关于我怎么能获得在C#中的字符串中的位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文