使用按位移位操作比较字符串

时间:2015-03-17 18:14:22

标签: c# bitwise-operators

所以我是csharp的新手,我似乎无法在这个程序中找到逻辑错误。我正在学习按位移位运算符,因为我是这些运算符的新手。我需要帮助来跟踪代码中的错误。程序对输入字符串进行编码并对编码后的字符串进行解码。之后我比较字符串以查看它们是否相等。它们似乎等于我,但是当我比较它时我会继续得到错误。这是我的代码:

 class Program
{
    static char[] transcode = new char[64];

    private static void prep()
    {
        for (int i = 0; i < transcode.Length; i++)
        {
            transcode[i] = (char)((int)'A' + i);

            if (i > 25 && i <= 51)
            {
                transcode[i] = (char)((int)transcode[i] + 6);

            }
            else if (i > 51)
            {
                transcode[i] = (char)((int)transcode[i] - 0x4b);
            }
        }

        transcode[transcode.Length - 3] = '+';
        transcode[transcode.Length - 2] = '/';
        transcode[transcode.Length - 1] = '=';

    }

    static void Main(string[] args)
    {
        prep();
        string test_string = "a";

        if (Convert.ToBoolean(String.Compare(test_string, decode(encode(test_string)))))
        {
            Console.WriteLine("Test succeeded");
        }
        else
        {
            Console.WriteLine("Test failed");
        }
    }


    private static string encode(string input)
    {

        int l = input.Length;
        int cb = (l / 3 + (Convert.ToBoolean(l % 3) ? 1 : 0)) * 4;// (0 +(1))*4  =4 
        char[] output = new char[cb];
        for (int i = 0; i < cb; i++)
        {
            output[i] = '=';
        }

        int c = 0;
        int reflex = 0;
        const int s = 0x3f;

        for (int j = 0; j < l; j++)
        {
            reflex <<= 8;
            reflex &= 0x00ffff00;
            reflex += input[j];
            int x = ((j % 3) + 1) * 2;

            int mask = s << x;
            while (mask >= s)
            {
                int pivot = (reflex & mask) >> x;
                output[c++] = transcode[pivot];
                char alpha = transcode[pivot];
                int invert = ~mask;
                reflex &= invert;
                mask >>= 6;
                x -= 6; //-4
            }
        }

        switch (l % 3)
        {
            case 1:
                reflex <<= 4; //16
                output[c++] = transcode[reflex];
                char at16 = transcode[16];
                // Console.WriteLine("Character at 16 is: " + at16);
                break;
            case 2:
                reflex <<= 2;
                output[c++] = transcode[reflex];
                break;
        }
        return new string(output);//final value is: YQ== (Encoded String.)
    }


    private static string decode(string input)//input is YQ== which has a length of 4
    {
        int l = input.Length;
        int cb = (l / 4 + ((Convert.ToBoolean(l % 4)) ? 1 : 0)) * 3 + 1; // (1 + (0))*4
        char[] output = new char[cb]; //4 in length      
        int c = 0;
        int bits = 0;
        int reflex = 0;
        for (int j = 0; j < l; j++)
        {
            reflex <<= 6;
            bits += 6;
            bool fTerminate = ('=' == input[j]);

            if (!fTerminate)
            {
                reflex += indexOf(input[j]);
                while (bits >= 8)
                {
                    int mask = 0x000000ff << (bits % 8);
                    output[c++] = (char)((reflex & mask) >> (bits % 8));    //convert issue cannot implicitly convert to proper data type.so will have to explicitly convert.
                    int invert = ~mask;
                    reflex &= invert;
                    bits -= 8;
                }
            }
            else
            {
                break;

            }
        }
        return new string(output);
    }

    private static int indexOf(char ch)
    {
        int index;
        for (index = 0; index < transcode.Length; index++)
            if (ch == transcode[index])
                break;
        return index;
    }
}

1 个答案:

答案 0 :(得分:1)

阅读String.Compare的文档,然后阅读Convert.ToBoolean的文档。当两个字符串相等时,请特别注意String.Compare返回的。然后比较该值如何通过ToBoolean

转换为布尔值

String.Compare专为排序字符串而设计。当两个字符串相等时,它返回0 ToBoolean会将0转换为false。因此,当字符串相等时,您的if评估为false而不是true

一个简单的改变是:

if (String.Compare(test_string, decode(encode(test_string)))==0)
{
    Console.WriteLine("Test succeeded");
}
else
{
    Console.WriteLine("Test failed");
}

@ Tom关于尾随空值的评论也适用,但似乎String.Compare只是忽略了它们。