Convert <boolean> to string

I have a boolean list with 92 booleans, I want the list to be converted to a string, I thought I would take 8 boolean (bits) and put them in bytes (8 bits), and then used ASCII to convert this byte value to char, then add characters to the string. However, after googeling for more than 2 hours, no luck. I tried converting the list to a list of bytes, but it did not 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 encode and decode a Boolean list in String? (Because I want people to share their logical list with a string, and not with a list of 90 1 and 0.)

EDIT: it works now! all for help

 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 will look at encoding 32 later, again for reference :)

+6
source share
4 answers

You must store your boolean values โ€‹โ€‹in BitArray .

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

Then you can convert BitArray to byte array

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

and an array of bytes to a Base64 string

 var result = Convert.ToBase64String(bytes); 

On the other hand, you can convert a Base64 string to an array of bytes

 var bytes2 = Convert.FromBase64String(result); 

and an array of bytes in BitArray

 var values2 = new BitArray(bytes2); 

The Base64 line looks like this: "Liwd7bRv6TMY2cNE" . This is probably a little inconvenient for exchanging people; look at base-32 human-oriented coding :

The intended use of these lines is [base-32] and-paste, text editing (for example, in HTML files), manual transcription via keyboard, manual transcription using pens and paper, vocal transcription over phone or radio, etc.

Desirable for such coding are:

  • minimization of transcription errors - for example, the well-known obfuscation problem '0' with 'O'
  • embedding in other structures - for example, search engines, structured or selected text, file systems, command shells
  • brevity - shorter [lines] are better than longer ones.
  • ergonomics. Human users (especially non-technical) should find the [strings] as simple and pleasant as possible. The more ugly [lines] look, the worse.
+13
source

For starters, it's a bad idea to StringBuilder strings in such a loop - 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> easy with LINQ, using the fact that string implements an IEnumerable<char> :

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

It's not clear where the byte array goes ... but you shouldn't try to represent arbitrary binary data in a string using Encoding.GetString . This is not for anything.

If you don't care what format your string uses, then using Base64 will work well, but keep in mind that if you group your boolean values โ€‹โ€‹into bytes, you will need additional information if you need to distinguish between "7 values" and "8 values the first of which is False. "

+1
source

Since I deduce from your code, you want a string with n digits 1 or 0 depending on the internal value of bool, and then how ...

 public override string ToString() { StringBuilder output = new StringBuilder(91); foreach(bool item in this.tempboolist) { output.Append(item ? "1" : "0"); } return output.ToString(); } 

Warning that this was not in the cuff, I have not yet confirmed this with the compiler!

+1
source

This function does what you want:

  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); } 
0
source

Source: https://habr.com/ru/post/908170/


All Articles