我得到了一个包含92个布尔值的布尔列表,我想将列表转换为字符串,我想我会取8个布尔值(位)并将它们放入字节(8位)然后使用ASCII转换它是char的字节值然后将字符添加到字符串。然而,经过超过2个小时的googeling,没有运气atm。我尝试将列表转换为字节列表,但它没有工作^^。
String strbyte = null;
for (int x = 0; x != tmpboolist.Count; x++) //tmpboolist is the 90+- boolean list
{
//this loop checks for true then puts a 1 or a 0 in the string(strbyte)
if (tmpboolist[x])
{
strbyte = strbyte + '1';
}
else
{
strbyte = strbyte + '0';
}
}
//here I try to convert the string to a byte list but no success
//no success because the testbytearray has the SAME size as the
//tmpboolist(but it should have less since 8 booleans should be 1 Byte)
//however all the 'Bytes' are 48 & 49 (which is 1 and 0 according to
//http://www.asciitable.com/)
Byte[] testbytearray = Encoding.Default.GetBytes(strbyte);
PS如果有人有更好的建议如何编码&将布尔列表解码为String? (因为我希望人们用字符串分享他们的布尔列表,而不是90 1和0的列表。)
编辑:现在就开始工作吧!所有的帮助string text = new string(tmpboolist.Select(x => x ? '1' : '0').ToArray());
byte[] bytes = getBitwiseByteArray(text); //http://stackoverflow.com/a/6756231/1184013
String Arraycode = Convert.ToBase64String(bytes);
System.Windows.MessageBox.Show(Arraycode);
//first it makes a string out of the boolean list then it uses the converter to make it an Byte[](array), then we use the base64 encoding to make the byte[] a String.(that can be decoded later)
我稍后会查看encoding32,再次查看所有帮助的内容:)
答案 0 :(得分:14)
您应该将布尔值存储在BitArray。
中var values = new BitArray(92);
values[0] = false;
values[1] = true;
values[2] = true;
...
然后你可以将BitArray转换为字节数组
var bytes = new byte[(values.Length + 7) / 8];
values.CopyTo(bytes);
和字节数组到Base64字符串
var result = Convert.ToBase64String(bytes);
相反,您可以将Base64字符串转换为字节数组
var bytes2 = Convert.FromBase64String(result);
和BitArray的字节数组
var values2 = new BitArray(bytes2);
Base64字符串如下所示:"Liwd7bRv6TMY2cNE"
。这对于人们之间的分享来说可能有点不方便;看看human-oriented base-32 encoding:
这些[base-32字符串]的预期用途包括 - 粘贴,文本编辑(例如在HTML文件中),通过手动转录 键盘,通过纸笔手动转录,声音转录结束 电话或收音机等
这种编码的需求是:
- 最小化转录错误 - 例如众所周知的混乱问题 “0”与“O”
- 嵌入其他结构 - 例如搜索引擎,结构化或 标记文本,文件系统,命令shell
- 简洁 - 较短的[字符串]比较长的字符串更好。
- 人体工程学 - 人类用户(尤其是非技术用户)应该找到 [字符串]尽可能轻松愉快。 [strings]看起来更丑陋,更糟糕。
答案 1 :(得分:1)
首先,在这样的循环中连接字符串是一个坏主意 - 至少使用StringBuilder
,或者在LINQ中使用类似的东西:
string text = new string(tmpboolist.Select(x => x ? '1' : '0').ToArray());
但使用List<bool>
实现string
的事实,LINQ将您的字符串转换为IEnumerable<char>
很容易:
List<bool> values = text.Select(c => c == '1').ToList();
不清楚字节数组的来源......但你should not try to represent arbitrary binary data in a string just using Encoding.GetString
。这不是它的用途。
如果你不关心你的字符串使用什么格式,那么使用Base64会很好 - 但要注意,如果你将布尔值分组为字节,如果你需要区分,你将需要额外的信息“ 7个值“和”8个值,其中第一个为False“,例如。
答案 2 :(得分:1)
由于我从您的代码推断出你需要一个n位为1或0的字符串,具体取决于内部列表的bool值,那么...
public override string ToString()
{
StringBuilder output = new StringBuilder(91);
foreach(bool item in this.tempboolist)
{
output.Append(item ? "1" : "0");
}
return output.ToString();
}
警告这不是袖口打字,我还没有用编译器验证这个!
答案 3 :(得分:0)
此功能可以满足您的需求:
public String convertBArrayToStr(bool[] input)
{
if (input == null)
return "";
int length = input.Count();
int byteArrayCount = (input.Count() - 1) / 8 + 1;
var bytes = new char[byteArrayCount];
for (int i = 0; i < length; i++ )
{
var mappedIndex = (i - 1) / 8;
bytes[mappedIndex] = (char)(2 * bytes[mappedIndex] +(input[i] == true ? 1 : 0));
}
return new string(bytes);
}