带有十六进制的神秘ushort数组 [英] mysterious ushort arrays with hex

查看:225
本文介绍了带有十六进制的神秘ushort数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

是的,因此下面是其他人的代码片段,这些人只是在使用它而无需理会它的工作方式.此代码是该类的一部分,可用于使用C#而不是使用interop生成Excel文件.我必须说它有效,但是,我对ushort []数组和发生的十六进制魔术有些困惑.请看一下,看看是否可以帮助我揭开它们的神秘面纱:

Hi folks,

right, so the following is a code snippet from someone else who is just using it without bothering how it does what it does. This code is a part of the class that can be used to generate Excel files using C# and NOT using interop. I must say it works, but, i m a bit brain-numbed by the ushort[] arrays and the hex-magic going on. Please have a look and see if you could help me demystify them:

public sealed class ExcelWriter : IDisposable
      {
            // private Stream stream;
            private readonly BinaryWriter writer;

            private readonly ushort[] begin = { 0x0809, 8, 0, 0x10, 0, 0 };
            private readonly ushort[] end = { 0x0A, 00 };

            public ExcelWriter(Stream stream)
            {
                  // this.stream = stream;
                  writer = new BinaryWriter(stream);
            }

            public void WriteCell(int row, int col, string value)
            {
                  ushort[] data = { 0x0204, 0, 0, 0, 0, 0 };
                  var len = value.Length;
                  var plainText = Encoding.ASCII.GetBytes(value);
                  data[1] = (ushort)(8 + len);
                  data[2] = (ushort)row;
                  data[3] = (ushort)col;
                  data[5] = (ushort)len;
                  WriteUshortArray(data);
                  writer.Write(plainText);
            }
...
}



我对解剖WriteCell()方法特别感兴趣,如果您愿意,它实际上是由main方法调用的,以便使用BinaryWriter和FileStream(相当标准的东西)从DataTable中写出值.我尝试过这样的事情,它是此代码的精简版和脱脂版:



I am interested in dissecting the WriteCell() method in particular, which is actually called by a main method, if u will, to write out values from a DataTable using BinaryWriter and FileStream (pretty much standard stuff). I tried something like this which is a heavily stripped down and de-fattened version of this code:

BinaryWriter bw = new BinaryWriter(new FileStream(@"Z:\temp.xls", FileMode.OpenOrCreate));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Write(ASCIIEncoding.ASCII.GetBytes("HELLO\r\n"));
            bw.Flush();
            bw.Close();



当我打开temp.xls时,它会打开并显示有关另一种格式的警告,但是我仍然可以在其中看到我的数据.所以我想像那些小的十六进制德鲁伊"正在做一些神秘的事情,从而在没有任何警告的情况下打开文件.

请不要将我指向十六进制的傻瓜"之类的网站,我知道它们是什么,我只想知道他们在Excel生成代码中到底在干什么,怎么做? :)

非常感谢...



When I open temp.xls, it opens with a warning about a different format but I can still see my data there. SO I imagine that those little "hexadecimal druids" are doing something mystical that opens the file without any warning.

Please don''t point me to "Hexadecimal for Dummies" kinda websites, i know what they are I just want to know what the hell they are doing in an Excel generation code and how? :)

Many thanks...

推荐答案

有人找到了文件格式(或对其进行了反向工程),而这些ushort值是告诉Excel开始新单元格的控制代码,等等.由于编写该代码的人没有留下对头文件的引用或对每个魔术数字的注释,因此实际上不可能推断出更多的内容.我没有Excel文件格式的来源,但是这就是您需要查找以找出为什么这些值就是它们的值的原因.
Someone has found (or reverse engineered) the file format and those ushort values are control codes that tell Excel to start a new cell etc. Since the guy that wrote that code didn''t leave a reference to a header file, or a comment about each magic number, it''s not really possible to infer more than that. I don''t have a source for the Excel file format, but that''s what you would need to go and look at to find out why those values are what they are.


这篇关于带有十六进制的神秘ushort数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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