Morano88
Morano88

Reputation: 2077

Converting text to bits (1's and 0's)

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#

Upvotes: 0

Views: 906

Answers (3)

Carter Medlin
Carter Medlin

Reputation: 12495

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;
    }

Upvotes: 0

SwDevMan81
SwDevMan81

Reputation: 50028

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).

Upvotes: 1

Femaref
Femaref

Reputation: 61497

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

Upvotes: 2

Related Questions