Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have the code below that read in a stream of characters from first to last in an orderly manner. I would like to process the information from last to first. Specifically from last bit(not byte) in the stream to first the first bit(not byte) in the stream. Binary Example: input = 110110110111000011110101, looking for output = 101011110000111011011011.

Can someone please show me how to read from very last bit in the stream to the very first bit in the stream?


C#
<pre>
using System;
using System.IO;
using System.Collections;

namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Folder99");
            if (!da.Exists)
            {
                Console.WriteLine("The folder '{0}' does not exist.", da.FullName);
                return;
            }
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                Console.WriteLine("There are no files in the folder '{0}'.", da.FullName);
                return;
            }
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);

            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));

            using (Stream input = File.OpenRead(filePath))
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bytesRead;
                while ((bytesRead = input.Read(buffer, 0, BufferSize)) > 0)
                {
                    for (int count = 0; count < bytesRead; count++)
                    {
                        byte theByte = buffer[count];
                        string theByteInBinary = Convert.ToString(theByte, 2).PadLeft(8, '0');
                        Console.WriteLine("{0} = {1}", theByteInBinary, theByte);
                    }
                    output.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}
Posted

1 solution

This is the key: https://msdn.microsoft.com/en-us/library/system.io.stream.seek%28v=vs.110%29.aspx[^].

Steams, by definition, are read from the beginning to the end. So, to invert bits, you need to read the stream (returning true from CanSeek. which is the case if you work with files, as in your example) in chunks an invert each chunk in memory. If the stream is small enough, it's not a problem to do it all in memory. For big streams, you have to to it all chunk by chunk. So, you have to read the stream size and plan the sizes and addresses of all chunks, and the read from the last to chunk to the first one. The inverted chunks can be written to the output stream immediately. This is pretty simple. I hope you don't need help in inverting chunks in memory.

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900