Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建奇数字节数组_C#_Arrays_Byte - Fatal编程技术网

C# 创建奇数字节数组

C# 创建奇数字节数组,c#,arrays,byte,C#,Arrays,Byte,我需要一些帮助来创建一个字节数组,该数组将允许以下内容: 字节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-结束:文件的内容 我已经有了将文件放入字节数组的以下代码,但这是最后一部分 byte[] filebytes; st.birth_certificate = detail[4]; downloadfile.HTML

我需要一些帮助来创建一个字节数组,该数组将允许以下内容:

  • 字节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-结束:文件的内容
我已经有了将文件放入字节数组的以下代码,但这是最后一部分

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

任何帮助都将不胜感激。

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

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\u name.pdf,字符串文件名行出现错误。我是否缺少某些内容检查filenameLength的值。长度可能大于文件大小。我不知道实际的字节数是向后的(小/大端),还是长度是ascii码。