转换列表<布尔值>到字符串布尔值>
我有一个包含 92 个布尔值的布尔列表,我希望将该列表转换为字符串,我想我将采用 8 个布尔值(位)并将它们放入一个字节(8 位)中,然后使用 ASCII 将其转换为字节value 为一个字符,然后将这些字符添加到一个字符串中。然而,在谷歌搜索了两个多小时后,没有运气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 如果有人对如何编码有更好的建议将布尔列表解码为字符串? (因为我希望人们用字符串分享他们的布尔列表,而不是 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)
我稍后会研究一下编码32,再次感谢所有帮助:)
I got a boolean list with 92 booleans, I want the list to be converted to a string, I thought I ll take 8 booleans(bits) and put them in a Byte(8 bits) and then use the ASCII to convert it the byte value to a char then add the chars to a string. However after googeling for more then 2 hours, no luck atm. I tried converting the List to a Byte list but it didn t work either ^^.
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 If anyone has a better suggestion on how to code & decode a Boolean list to a String?
(Because I want people to share their boolean list with a string rather then a list of 90 1 and 0s.)
EDIT: got it working now! ty all for helping
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)
I ll look into the encoding32 later, ty for all the help again :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该将布尔值存储在 BitArray 中。
然后,您可以将 BitArray 转换为字节数组
,并将字节数组转换为 Base64 字符串。
相反,您可以将 Base64 字符串转换为字节数组
,并将字节数组转换为 BitArray
Base64 字符串如下所示:
"Liwd7bRv6TMY2cNE"
。这对于人与人之间的共享来说可能有点不方便;看看面向人类的base-32编码:You should store your boolean values in a BitArray.
Then you can convert the BitArray to a byte array
and the byte array to a Base64 string
Reversely, you can convert a Base64 string to a byte array
and the byte array to a BitArray
The Base64 string looks like this:
"Liwd7bRv6TMY2cNE"
. This is probably a bit unhandy for sharing between people; have a look at human-oriented base-32 encoding:首先,在这样的循环中连接字符串不是一个好主意 - 至少使用
StringBuilder
,或者在 LINQ 中使用类似的东西: 使用 LINQ 很容易,使用
string
实现IEnumerable
的事实:不清楚字节数组来自哪里......但是你不应该 尝试仅使用
Encoding.GetString
在字符串中表示任意二进制数据。那不是它的目的。如果您不关心字符串使用什么格式,那么使用 Base64 会很好 - 但请注意,如果您将布尔值分组为字节,如果您需要区分“7 个值”,则需要额外的信息例如“8 个值,其中第一个为 False”。
To start with, it's a bad idea to concatenate strings in a loop like that - at least use
StringBuilder
, or use something like this with LINQ:But converting your string to a
List<bool>
is easy with LINQ, using the fact thatstring
implementsIEnumerable<char>
:It's not clear where the byte array comes in... but you should not try to represent arbitrary binary data in a string just using
Encoding.GetString
. That's not what it's for.If you don't care what format your string uses, then using Base64 will work well - but be aware that if you're grouping your Boolean values into bytes, you'll need extra information if you need to distinguish between "7 values" and "8 values, the first of which is False" for example.
因为我从你的代码中推断你想要一个包含 n 位数字 1 或 0 的字符串,具体取决于内部列表 bool 值,那么怎么样...
警告这是即兴输入,我还没有使用编译器验证它!
Since I am infering from your code you want a string with n digits of either 1 or 0 depending onthe internal lists bool value then how about...
Warning this was off the cuff typing, I have not validated this with a compiler yet!
这个函数可以实现你想要的功能:
This function does what you want: