【问题标题】:Create an odd byte array创建奇数字节数组
【发布时间】:2017-07-19 04:31:11
【问题描述】:

我正在寻求一些帮助来创建一个允许以下内容的字节数组:

  • bytes 1-2 : 整数 n,指定文件名的长度
  • 3 - n+2 : 文件名
  • n+3 - n+10 : 文件的最后修改日期
  • n+11 - n+12 : 值为 1 的整数
  • n+13 - n+16 : 文件数据长度的长整数
  • n+17 - n+20 : 长整数,值为 0
  • n+21 - end : 文件的内容。

我已经有以下代码将文件放入字节数组,但这是在最后一部分。

byte[] filebytes;
st.birth_certificate = detail[4];
downloadfile.HTML = detail[4];
downloadfile.fileName = downloadfile.GetFileNameFromUrl(st.birth_certificate);
downloadfile.toLocation = @"c:\temp\" + downloadfile.fileName;
if (downloadfile.DownloadFile())
{
    filebytes= File.ReadAllBytes(downloadfile.toLocation);
    st.birth_certificate_file = filebytes;
}

任何帮助将不胜感激。

【问题讨论】:

    标签: c# arrays byte


    【解决方案1】:

    最好使用 BinaryReader。我不确定数字是十六进制值还是 ascii 数字(或 Big/Little Endian),所以我在做一些猜测。代码可能需要一些小的调整:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string URL = "enter you url here";
                FileStream sReader = File.OpenRead(URL);
                BinaryReader reader = new BinaryReader(sReader);
    
                int filenameLength = reader.ReadInt16();
                string filename = Encoding.UTF8.GetString(reader.ReadBytes(filenameLength));
                int year = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(4)));
                int month = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(2)));
                int day = int.Parse(Encoding.UTF8.GetString(reader.ReadBytes(2)));
                DateTime date = new DateTime(year, month, day);
                short number1 = reader.ReadInt16();
                int number2 = reader.ReadInt32();
                byte[] data = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position + 1));
    
            }
    
    
        }
    }
    

    【讨论】:

    • 好的,我已经完全按照所示复制了您的代码,因为它似乎不起作用。我已将“在此处输入您的 URL”替换为 c:\\temp\\file_name.pdf,并且在字符串文件名行出现错误。我错过了什么
    • 检查文件名长度的值。长度可能大于文件大小。我不知道字节的实际数字是向后(小/大端)还是长度是ascii。
    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 2015-11-19
    • 2013-09-23
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多