Click here to Skip to main content
14,360,938 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hey guys,
Ok, i have a question I need to answer and cant figure out what is wrong here:
Here it is:

Please take a look at the following code. Then fix and improve the code to the level that you would consider to be production standard.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

        private static void prep()
        {                        
            for (int i = 0; i < 64; i++)  
            {    
                transcode[i] = (char)((int)'A' + i);                
                if (i > 25) transcode[i] = (char)((int)transcode[i] + 6 );
                if (i > 51) transcode[i] = (char)((int)transcode[i] - 0x4b);
            }
            transcode[62] = '+';
            transcode[63] = '/';            
            transcode[64] = '=';        
        }

        static void Main(string[] args) 
        {            
            prep();
  
            string test_string = "This is it";

            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;   

            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];
                    int invert = ~mask;
                    reflex &= invert;
                    mask >>= 6;
                    x -= 6;
                }
            }
 
            switch (l%3)
            {
                case 1:
                    reflex <<= 4;
                    output[c++] = transcode[reflex]; 
break;                   
                
                case 2:
                    reflex <<= 2;
                    output[c++] = transcode[reflex];
                    break;
                
            }
            Console.WriteLine("{0} --> {1}\n", input, new string( output) );
            return new string( output );
        }


        private static string decode(string input)
        {
            int l = input.Length;
            int cb = (l/4 + ((Convert.ToBoolean(l%4))?1:0))*3+1;   
            char[] output = new char[cb];        
            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++] = (reflex & mask) >> (bits % 8);    
                    int invert = ~mask;
                    reflex &= invert;
                    bits -= 8;
                }
 
                if (fTerminate)
                    break;
            }
            Console.WriteLine("{0} --> {1}\n", input, new string( output ));
            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;
        }
    }
}


Problem here is, the decoding works, but it sometimes gives me an Ain front or /0's at the back and I cannot seem to figure out why.
Thanks
Posted
Updated 21-Nov-12 1:36am
v2
Comments
Daniele Rota Nodari 27-Nov-12 10:05am
   
Why are you converting int to bool using Convert.ToBoolean ? You should directly test for the int value using ==,!=,<,>,<=,>= operators !
More.. I see a "switch (l%3)" with only "case 1" and "case 2", so case 0 is ignored: is it correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100