从最后一位到第一位读二进制 [英] Reading Binary from Last Bit to First Bit

查看:113
本文介绍了从最后一位到第一位读二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码以有序的方式从头到尾读取字符流。我想处理从最后到第一个的信息。特别是从流中的最后一位(不是字节)到流中的第一位(不是字节)。二进制示例:输入= 110110110111000011110101,寻找输出= 101011110000111011011011.



有人可以告诉我如何从流中的最后一位读到第一位溪流?





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?


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

推荐答案

这关键是: https:// msdn。 microsoft.com/en-us/library/system.io.stream.seek%28v=vs.110%29.aspx [ ^ ]。



根据定义,Steams从头到尾读取。因此,要反转位,您需要读取流(从 CanSeek 返回true。如果您使用文件,就像在您的示例中一样),以块为单位反转内存中的每个块。如果流足够小,那么在内存中完成所有操作并不是问题。对于大流量来说,你必须要大块。因此,您必须读取流大小并计划所有块的大小和地址,以及从最后一个块到第一个块的读取。可以立即将反转的块写入输出流。这很简单。我希望你在内存中反转块时不需要帮助。



-SA
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


这篇关于从最后一位到第一位读二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆