转换列表<布尔>为String [英] Convert List<boolean> to String

查看:134
本文介绍了转换列表<布尔>为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我买了92布尔一个布尔值列表,我想要列表转换为字符串,我想我会采取8布尔(位),然后把它们放在一个字节(8位),然后使用ASCII转换它字节值为char然后将字符添加到字符串。但是之后googeling了超过2个小时,没有运气大气压。我尝试了列表转换为字节列表中,但没科技工作无论是^^。

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如果任何人有关于如何code和更好的建议;德code布尔列表为String?
(因为我希望人们能共享一个字符串的布尔列表,而随后的90 1和0的清单。)

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.)

编辑:得到它现在的工作! TY所有帮助

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)

我会考虑的encoding32后,TY所有帮助再次:)

I ll look into the encoding32 later, ty for all the help again :)

推荐答案

您应该存储在一个的 BitArray

var values = new BitArray(92);
values[0] = false;
values[1] = true;
values[2] = true;
...


然后您可以将BitArray转换为字节数组


Then you can convert the BitArray to a byte array

var bytes = new byte[(values.Length + 7) / 8];
values.CopyTo(bytes);

和字节数组到Base64字符串

and the byte array to a Base64 string

var result = Convert.ToBase64String(bytes);

相反地,你可以一个Base64字符串转换为字节数组

Reversely, you can convert a Base64 string to a byte array

var bytes2 = Convert.FromBase64String(result);

和字节数组到BitArray

and the byte array to a BitArray

var values2 = new BitArray(bytes2);


中的Base64字符串看起来像这样:Liwd7bRv6TMY2cNE。这可能是有点不方便的人之间的共享;看看以人为本的基本编码32


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:

预计这些[基32字符串]的用途包括cut-
  和粘贴,文本编辑(例如,在HTML文件),通过手动转录
  键盘,通过笔和纸,过声乐转录手工抄写
  手机或无线电等。

Anticipated uses of these [base-32 strings] include cut- and-paste, text editing (e.g. in HTML files), manual transcription via a keyboard, manual transcription via pen-and-paper, vocal transcription over phone or radio, etc.

对于这样一个编码的必要条件是:

The desiderata for such an encoding are:


      
  • 减少抄写错误 - 例如令人困惑的众所周知的问题
      '0'与'O'

  •   
  • 嵌入到其他结构 - 例如搜索引擎,结构化或
      标记文本,文件系统,命令shell

  •   
  • 简洁 - 短[字符串]是比长的好

  •   
  • 的人体工程学设计 - 人类用户(尤其是非技术性的)应该找到
      [字符串]为方便和愉快。在[字符串]长相丑陋的,坏。

  •   

这篇关于转换列表<布尔>为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆