views:

102

answers:

3

I'm implementing an MD-5 Hashing algorithm and I want to convert the text I have to bits so I can start manipulating them. As you know Hashing require taking block of bits and then manipulating them. There are many ways to do that but I can't determine the best/easiest way to just convert the text (string) into array of bits. Any clue ? In C#

+2  A: 

Encoding.GetBytes(string s) see msdn. Of course, you have to select a fitting encoding, depending on the encoding you want.

Femaref
+1  A: 

Once you use the Encoding.GetBytes(string s) as suggested, you could then pass the byte[] to the constructor of the BitArray class:

Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).

SwDevMan81
A: 

This is what you were asking for.

    protected void Page_Load(object sender, EventArgs e)
    {
        var x = GetBits("0101010111010010101001010");
    }

    private bool[] GetBits(string sBits)
    {
        bool[] aBits = new bool[sBits.Length];

        for (var i = 0; i < aBits.Length; i++)
        {
            aBits[i] = sBits[i] == '1';
        }

        return aBits;
    }
Carter