Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DCing algorithm
#15
(03-26-2015, 07:58 AM)YinYin Wrote:  
    CSHARP-Code:
public class ReadFile
    {
        public static string main(String FileName)
        {
            string text = "";
            StreamReader finp = new StreamReader(FileName);
            int counta = 0;
            int countTo123 = 0;
            byte b;
            byte d;
            while (finp.Peek() >= 0) 
            {
                countTo123++;
                b = (byte)finp.Read();
                if (countTo123 > 123)
                {
 
                    d = DecryptByte(counta, b);
                    counta++;
                    if (counta > 36)
                    {
                        counta = 0;
                    }
                    text += (char)d;
                }
            }
            finp.Close();
            return text;
        }
 
        public static byte DecryptByte(int counta, byte b)
        {
            String plainEncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
            b -= (byte)plainEncryptionKey[counta];
            return b;
        }
    }
Your code is wrong.
StreamReader.Read() returns the next character read, which is not necessarily the same as the next byte. Using a FileStream instead of StreamReader solves this as it has a ReadByte() method, which returns the next byte or -1 if the end-of-file has been reached.
I'll also point out that calling both Peek() and Read() is both redundant and slow, and the same thing goes for reading the first 123 characters only to not do anything with them. FileStream.Seek() is a better solution.
My version of the code:
    CSHARP-Code:
public class ReadFile
{
    public static string main(String FileName)
    {
        string text = "";
        FileStream finp = new FileStream(FileName,FileMode.Open);
        int counta = 0;
        int b;
        byte d;
        finp.Seek(123,SeekOrigin.Begin);
        while((b = finp.ReadByte()) != -1)
        {
            d = DecryptByte(counta, (byte)b);
            counta++;
            if (counta > 36)
            {
                counta = 0;
            }
            text += (char)d;
        }
        finp.Close();
        return text;
    }
 
    public static byte DecryptByte(int counta, byte b)
    {
        String plainEncryptionKey = "odBearBecauseHeIsVeryGoodSiuHungIsAGo";
        b -= (byte)plainEncryptionKey[counta];
        return b;
    }
}

I would also like to add than C♯ is a dumb language, and that C++ is way better.
Age ratings for movies and games (and similar) have never been a good idea.
One can learn a lot from reinventing wheels.
An unsound argument is not the same as an invalid one.
volatile in C++ does not mean thread-safe.
Do not make APIs unnecessarily asynchronous.
Make C++ operator > again
Trump is an idiot.
Reply
Thanks given by: YinYin


Messages In This Thread
DCing algorithm - by Silverthorn - 01-14-2009, 07:33 PM
RE: DCing algorithm - by Boop - 01-14-2009, 08:23 PM
RE: DCing algorithm - by YinYin - 03-26-2015, 07:58 AM
RE: DCing algorithm - by Som1Lse - 03-26-2015, 12:40 PM
RE: DCing algorithm - by YinYin - 03-26-2015, 12:57 PM
RE: DCing algorithm - by Silverthorn - 01-14-2009, 08:26 PM
RE: DCing algorithm - by Azriel - 01-15-2009, 06:09 PM
RE: DCing algorithm - by Boop - 01-15-2009, 06:29 PM
RE: DCing algorithm - by 1477 - 01-15-2009, 09:50 PM
RE: DCing algorithm - by Boop - 01-15-2009, 11:09 PM
RE: DCing algorithm - by Silverthorn - 01-16-2009, 03:06 PM
RE: DCing algorithm - by Yakui - 01-18-2009, 09:19 PM
RE: DCing algorithm - by Kevin - 08-01-2012, 10:45 AM
RE: DCing algorithm - by Boop - 08-01-2012, 11:53 AM
RE: DCing algorithm - by A-Man - 03-26-2015, 08:11 AM
RE: DCing algorithm - by YinYin - 03-26-2015, 08:31 AM
RE: DCing algorithm - by A-Man - 03-26-2015, 02:14 PM
RE: DCing algorithm - by Dia6lo - 06-19-2015, 11:57 AM
RE: DCing algorithm - by Som1Lse - 06-19-2015, 05:14 PM
RE: DCing algorithm - by Dia6lo - 06-19-2015, 05:37 PM
RE: DCing algorithm - by Hellblazer - 06-21-2015, 10:33 AM
RE: DCing algorithm - by Zelphir - 06-13-2021, 10:38 AM
RE: DCing algorithm - by Mesujin - 08-30-2022, 03:47 PM



Users browsing this thread: 1 Guest(s)