0

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?

5
  • 1
    Needs more details. What do you want to do exactly with those bits? Do you want to write them as a string? Commented Oct 5, 2010 at 17:03
  • 1
    A .NET char is 16 bit, you might be more interested in bytes Commented 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! Commented Oct 5, 2010 at 17:06
  • Is this C# question? I smell C++ background here. :) Commented Oct 5, 2010 at 17:09
  • unfortunately i need to work with bit's in C#=) Commented Oct 5, 2010 at 17:15

2 Answers 2

1

The best way to store bits in C# is in the BitArray class, if you just need them as bits. If you need the integer value of the 18 bits, then you have to convert them to int or double or whatever.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.