开发者

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 ma开发者_JS百科nipulating 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#


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜