1

I'm utilizing the algorithm of this question: Convert List<boolean> to String (the choosen answer)

But I'm dealing with the length of the BitArray when this is reversed. I mean if my BitArray length was 12, when I reverse it I need to have the length 12 and not 16.

I can imagine I need to add the count information into the string result. But I'm not sure if this would be right. How could I get the same bitArray with the same length?

Current Code:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var values = new BitArray(12);
    values[0] = true;
    values[1] = true;
    values[2] = true;
    values[3] = true;
    values[4] = true;
    values[5] = true;
    values[6] = true;
    values[7] = true;
    values[8] = false;
    values[9] = true;
    values[10] = true;
    values[11] = false;

    var bytes = new byte[(values.Length + 7) / 8];
    values.CopyTo(bytes, 0);
    var result = Convert.ToBase64String(bytes);

    var bytes2 = Convert.FromBase64String(result);
    var values2 = new BitArray(12);

    for (int i = 0; i < values2.Count; i++)
    {
        var temp = bytes - 1;
    }
}
2
  • 2
    please show the code that you have in regards to the error or issue that you are running into.. Commented Jan 7, 2013 at 18:19
  • Have you looked at the BitArrayToStr() Method..? Commented Jan 7, 2013 at 18:24

3 Answers 3

1

Why not just do:

bool[] vals = new[]{false, true, false, true, false, true};

StringBuilder sb = new StringBuilder();
foreach(var b in vals){
    sb.Append(b ? "1" : "0");
}
sb.ToString(); 

// prints out "010101"

EDIT: Wasn't sure if you had a bool[] initially or a BitArray. The method will just as well for BitArray:

var values = new BitArray(12);
values[0] = true;
values[1] = true;
values[2] = true;
values[3] = true;
values[4] = true;
values[5] = true;
values[6] = true;
values[7] = true;
values[8] = false;
values[9] = true;
values[10] = true;
values[11] = false;

StringBuilder sb = new StringBuilder();
foreach(var b in values){
    sb.Append((bool)b ? "1" : "0");
}
sb.ToString();

// prints out "111111110110"
Sign up to request clarification or add additional context in comments.

2 Comments

well, I plan to have in that array like 300 bool items, that's the reason way I was trying to compress it throgh a BitArray
The algorithm will work regardless. Are you looking for a specific performance baseline?
0

I do not think there is a way to avoid the padding of 0s, and it will become an issue when you try to reverse the array, I think the way around this is to create your own type, that will have a variable storing the number of bytes, and this should allow you to do code to ignore the padding when doing the reverse or other operations.

You can store the number of padded bits before doing values.CopyTo(bytes, 0); , and then after doing var bytes2 = Convert.FromBase64String(result); , you can do bitshifting with the appropriate number of bits (to trim the padded bits)

Edit:

You can do bitshifting on the resulting bitarray:

public static BitArray ShiftRight(this BitArray instance)
{
    return new BitArray(new bool[] { false }.Concat(instance.Cast<bool>().Take(instance.Length - 1)).ToArray());
}

(taken from https://stackoverflow.com/a/7696793/1279594 )

Comments

0

While converting back using Convert.FromBase64String(), you get a byte array of length 2 bytes, which is 16 bits. Now, when you intially created the byte array from the BitArray, although the length of converted ByteArray was 2 bytes ie 16 bits again, however, your actual BitArray was of length 12 bits. This fact is known to you and not the underlying implementation which considers the byte array of length 16 and creates the BitArray ,therefore, of length 16.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.