目标是将字节数组写入文件。我有一些字节的字节数组fits [],然后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace _32_to_16
{
class Program
{
static void Main(string[] args)
{
byte[] fits = File.ReadAllBytes("1.myf");
byte[] img = new byte[fits.Length / 2];
for (int i = 0; i < fits.Length; i += 4) //Drops 2 high bytes
{
img[i/2] = fits[i + 2];
img[i/2 + 1] = fits[i + 3];
}
File.WriteAllBytes("new.myf", img);
}
}
}
在写入文件之前,img []具有相同的值:
写入文件后,在十六进制编辑器中,我看到
有时,使用其他fits []值,img []数组可以正确地写入文件。我做错了什么?
测试文件1.myf(导致错误的结果)https://www.dropbox.com/s/6xyf761oqm8j7y1/1.myf?dl=0测试文件2.myf(正确写入文件)https:// www .dropbox.com / s / zrglpx7kmpydurz / 2.myf?dl = 0
我简化了代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace _32_to_16
{
class Program
{
static void Main(string[] args)
{
byte[] img_correct = new byte[8] { 0xbd, 0x19, 0xbd, 0x72, 0xbd, 0x93, 0xbd, 0xf7 };
File.WriteAllBytes("img_correct.myf", img_correct);
byte[] img_strange = new byte[8] { 0x33, 0x08, 0x33, 0xac, 0x33, 0xe3, 0x33, 0x94 };
File.WriteAllBytes("img_strange.myf", img_strange);
}
}
}
在十六进制编辑器img_correct.myf中看起来像这样:bd 19 bd 72 bd 93 bd f7
在十六进制编辑器img_strange.myf中看起来像这样:33 08 33 3f 3f 3f
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句