I need to transfrom bits into char array or string, help to find best way to store bits and what shoud I do if for example I have 18 bits I will make 2 char and 2 bits?
-
1Needs more details. What do you want to do exactly with those bits? Do you want to write them as a string?Etienne de Martel– Etienne de Martel2010-10-05 17:03:43 +00:00Commented Oct 5, 2010 at 17:03
-
1A .NET char is 16 bit, you might be more interested in bytesAlbin Sunnanbo– Albin Sunnanbo2010-10-05 17:05:48 +00:00Commented Oct 5, 2010 at 17:05
-
briffly i'm trying to save bit's into file but as far as i understand i only can save byte so i'm trying to get char from bit's and save them!ZAA– ZAA2010-10-05 17:06:49 +00:00Commented Oct 5, 2010 at 17:06
-
Is this C# question? I smell C++ background here. :)Nayan– Nayan2010-10-05 17:09:34 +00:00Commented Oct 5, 2010 at 17:09
-
unfortunately i need to work with bit's in C#=)ZAA– ZAA2010-10-05 17:15:52 +00:00Commented Oct 5, 2010 at 17:15
Add a comment
|
2 Answers
First step would be to convert your bit array into bytes and once you have an array of bytes you will need to choose a proper encoding and convert to a string which is an array of chars:
BitArray bitArray = new BitArray(new[] { true, false, true, false, });
byte[] bytes = new byte[bitArray.Length];
bitArray.CopyTo(bytes, 0);
char[] result = Encoding.UTF8.GetString(bytes).ToCharArray();
Obviously you need to know the encoding of those bits in order to be able to convert to characters. If you don't know the encoding you should reconsider what you are trying to do.