看不到 NULL 终止符 [英] Can't see past the NULL Terminator

查看:63
本文介绍了看不到 NULL 终止符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此苦苦挣扎了一段时间.我创建了一个实用程序,允许您打开 .TXT 文件.这些文本文件包含 PCL(打印命令语言).当我导入一个新文件时,它被一个 \0(NULL 终止符)截断.因为 PCL 文件在我导入的所有内容中随机包含图形图像,所以在第一个位图图像处被截断,因为位图图像以 NULL 开头.

I've been struggling with this for a while. I created a utility that allows you to open .TXT files. These text files contain PCL (Print Command Language). When I import a new file, it is being truncated by a \0 (NULL Terminator character). Because PCL files contain graphic images randomly throughout everything I import is truncated at the first bitmap image because the bitmap images start with NULL.

这是这里看到的确切问题:使用 TextBox 或 RichTextBox 显示图像文件中的原始数据?

This is the exact issue as seen here: Displaying Raw Data From Image File Using TextBox or RichTextBox?

不幸的是,由于我的低(新手)声誉(需要 15 个代表),我无法对此线程发表评论.也无法粘贴屏幕截图(需要 10 次重复).

Unfortunately I can't comment on this thread though because of my low (newbie) reputation (need 15 rep). Also can't paste a screenshot (need 10 rep).

以下是 Notepad++ 显示信息的方式:

下面是我的 RichTextBox 显示相同信息的方式:

这是一个问题的原因(缩小):

栅格数据正好位于我需要的两部分数据之间(PCL).栅格数据下方的所有信息都不会被引入.

The raster data is right between two sections of data I need (The PCL). All of the information below the raster data won't pull in.

这是我尝试过的(注意:我使用的是自定义 RichTextBox,但这不会影响任何事情,因为它只是一个具有拖放功能的 RichTextBox):

Here is what I have tried (Note: I am using a custom RichTextBox, but this shouldn't affect anything since it's just a RichTextBox with Drag/Drop functionality):

byte[] bytes = new byte[2048];
string data = System.Text.Encoding.ASCII.GetString(bytes);
dragDropRichTextBox1.Text = data.Replace("\0", @"1");

这只会导致一连串 2048 个数字1"字符,没有任何文本文件的数据被拉入.非常感谢任何帮助.

This just causes a chain of 2048 number "1" characters with none of the text file's data pulling in. Any help is much appreciated.

无论我做什么,我都希望保留当前的拖放功能:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PCL_Utility
{
    public class DragDropRichTextBox : RichTextBox
    {
        public DragDropRichTextBox()
        {
            this.AllowDrop = true;
            this.DragDrop += DragDropRichTextBox_DragDrop;
        }

        void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
        {
            //string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];
            string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];

            if (fileText != null)
            {
                foreach (string name in fileText)
                {
                    try
                    {
                        this.AppendText(File.ReadAllText(name) + "\n -------- End of File -------- \n\n");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);  
                    }
                }
            }
        }
    }
}

推荐答案

与其尝试将文件数据读入字符串,不如 Jim Mischel 回答说应该将其读入字节数组并进行处理.

Rather than trying to read the file data into a string, as Jim Mischel answered it should be read into a byte array and processed.

这是一个静态类,它会将文件读入字节数组,并根据字典查找进行处理.对于不可打印的 ASCII 字符和超过 127 的所有值,我已经用\00"预先填充了字典.

Here's a static class that will read a file into a byte array, and process it based on a dictionary lookup. I've prepopulated the dictionary with "\00" for non-printable ASCII characters and all values over 127.

    public static class BinaryFile
    {

        private static string[] __byteLookup = new string[256];

        static BinaryFile()
        {
            // Display printable ASCII characters as-is
            for (int i = 0x20; i < 0x7F; i++) { __byteLookup[i] = ((char)i).ToString(); }

            // Display non-printable ASCII characters as \{byte value}
            for (int i = 0; i < 0x20; i++) { __byteLookup[i] = "\\" + i.ToString();}
            for (int i = 0x7F; i <= 0xFF; i++) { __byteLookup[i] = "\\" + i.ToString(); }

            // Replace pre-populated values with custom values here if desired.
        }

        public static string ReadString(string filename)
        {
            byte[] fileBytes = System.IO.File.ReadAllBytes(filename);

            return String.Join("", (from i in fileBytes select __byteLookup[i]).ToArray());
        }
    }

编辑,因为您想将它与自定义拖放代码一起使用,用法应该是:

Edit since you want to use this with your custom drag-and-drop code, the usage should be:

   void DragDropRichTextBox_DragDrop(object sender, DragEventArgs e)
    {
        string[] fileText = e.Data.GetData(DataFormats.FileDrop) as string[];

        if (fileText != null)
        {
            foreach (string name in fileText)
            {
                try
                {
                    // Read each file using the helper class rather than File.ReadAllText
                    // then append the end-of-file line
                    this.AppendText(BinaryFile.ReadString("your_file_name.txt") 
                        + "\n -------- End of File -------- \n\n");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);  
                }
            }
        }
    }

这篇关于看不到 NULL 终止符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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