Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<boolean> to String

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

like image 956
Maximc Avatar asked Feb 10 '12 18:02

Maximc


2 Answers

You should store your boolean values in a BitArray.

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

Then you can convert the BitArray to a byte array

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

and the byte array to a Base64 string

var result = Convert.ToBase64String(bytes);

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

var bytes2 = Convert.FromBase64String(result);

and the byte array to a BitArray

var values2 = new BitArray(bytes2);

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:

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:

  • minimizing transcription errors -- e.g. the well-known problem of confusing '0' with 'O'
  • embedding into other structures -- e.g. search engines, structured or marked-up text, file systems, command shells
  • brevity -- Shorter [strings] are better than longer ones.
  • ergonomics -- Human users (especially non-technical ones) should find the [strings] as easy and pleasant as possible. The uglier the [strings] looks, the worse.
like image 75
dtb Avatar answered Sep 27 '22 23:09

dtb


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:

string text = new string(tmpboolist.Select(x => x ? '1' : '0').ToArray());

But converting your string to a List<bool> is easy with LINQ, using the fact that string implements IEnumerable<char>:

List<bool> values = text.Select(c => c == '1').ToList();

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.

like image 40
Jon Skeet Avatar answered Sep 27 '22 23:09

Jon Skeet