c# – Strange Notepad HEX-editor插件

前端之家收集整理的这篇文章主要介绍了c# – Strange Notepad HEX-editor插件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目标是将字节数组写入文件.
我有字节数组[]与一些字节,然后:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _32_to_16
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. byte[] fits = File.ReadAllBytes("1.myf");
  14. byte[] img = new byte[fits.Length / 2];
  15. for (int i = 0; i < fits.Length; i += 4) //Drops 2 high bytes
  16. {
  17. img[i/2] = fits[i + 2];
  18. img[i/2 + 1] = fits[i + 3];
  19. }
  20. File.WriteAllBytes("new.myf",img);
  21. }
  22. }
  23. }

在写入文件之前,img []具有相同的值:

> img [0] = 0x31
> img [1] = 0x27
> img [2] = 0x31
> img [3] = 0xe2
>依此类推……

写入文件后,在HEX编辑器中我看到了

> 00000000:31 27 31 3f和其他错误值.

有时,使用其他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

我简化了代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace _32_to_16
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. byte[] img_correct = new byte[8] { 0xbd,0x19,0xbd,0x72,0x93,0xf7 };
  14. File.WriteAllBytes("img_correct.myf",img_correct);
  15.  
  16. byte[] img_strange = new byte[8] { 0x33,0x08,0x33,0xac,0xe3,0x94 };
  17. File.WriteAllBytes("img_strange.myf",img_strange);
  18. }
  19. }
  20. }

在HEX-editor img_correct.myf看起来像这样:
bd 19 bd 72 bd 93 bd f7

在HEX-editor中,img_strange.myf看起来像这样:
33 08 33 3f 3f 3f

解决方法

您正在使用记事本中的HEX-Editor插件,该插件似乎有一个 problem reading binary files.

尝试使用其他十六进制编辑器,它应显示正确的值.

这是HxD和HEX-Editor的屏幕截图,显示相同的文件

猜你在找的C#相关文章