JJDev
JJDev

Reputation: 173

Compare string using bitwise shift operation

So i am new to csharp and i cant seem to find a logical error here in this program.i am learning the bitwise shift operators as i am new to these operators. I need help tracing a fault in my code. the program encodes an input String and decodes the encoded String after.After that i compare the string to see if they are equal.They seem to be equal to me but i keep getting a false when i compare them. Here is my code:

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

Upvotes: 0

Views: 1577

Answers (1)

Matt Burland
Matt Burland

Reputation: 45155

Read the docs for String.Compare then read the docs for Convert.ToBoolean. Pay particular attention to the value returned by String.Compare when two strings are equal. Then compare with how that value gets converted to a boolean by ToBoolean

String.Compare is designed for sorting strings. It returns 0 when two strings are equal. ToBoolean will convert that 0 to false. So when you strings are equal, your if evaluates to false and not true.

A simple change would be:

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

@Tom's comment about the trailing nulls also applies, but it seems that String.Compare just ignores them.

Upvotes: 1

Related Questions