使用移位操作比较字符串

Compare string using bitwise shift operation

所以我是 csharp 的新手,我似乎无法在这里找到逻辑错误 program.i 我正在学习位移运算符,因为我是这些运算符的新手。我需要帮助来追踪我的代码中的错误。该程序对输入字符串进行编码并对编码字符串 after.After 进行解码,我比较字符串以查看它们是否 equal.They 似乎与我相等,但是当我比较它们时我总是得到错误。这是我的代码:

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

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

转换为布尔值

String.Compare 专为 排序 字符串而设计。它returns 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 只是忽略了它们。