Reputation: 73
The goal is to write byte array to file. I have byte array fits[] with some bytes and then:
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);
}
}
}
Before writing to the file img[] has same values:
After writing to file, in HEX editor i see
Sometimes, with other fits[] values, img[] array write correctly to file. What I`m doing wrong?
File for test 1.myf (which makes wrong results) https://www.dropbox.com/s/6xyf761oqm8j7y1/1.myf?dl=0
File for test 2.myf (correct writes to file) https://www.dropbox.com/s/zrglpx7kmpydurz/2.myf?dl=0
I simplified the code:
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);
}
}
}
in HEX-editor img_correct.myf looks like this: bd 19 bd 72 bd 93 bd f7
in HEX-editor img_strange.myf looks like this: 33 08 33 3f 3f 3f
Upvotes: 7
Views: 3861
Reputation: 21
For Full Width Colon ":" the correct Unicode format is : U+EF1A
But in NotePad ++ the ":" in Hex Editor show "EFBC9A" and not "EF1A".
Because this is an UTF8 Encoding & this is not in the Unicode format.
If I put "EFBC9A" in another editor, it show the Korean character "벚".
When you direct type in Hex Editor, make sure to use UTF8 encoding, but when you are not in Hex Editor, make sue to use Unicode Format and not UTF8 encoding.
So people is confusing with UTF8 Encoding and Unicode format.
By the way: the U+EF1A --> ":" can be put in folder name in Windows System.
Upvotes: 2
Reputation: 3513
You're using the HEX-Editor plugin from Notepad++, which seems to have a problem reading binary files.
Try with another hex editor and it should display the correct values.
Here's a screenshot of HxD and HEX-Editor displaying the same file
Upvotes: 10
Reputation: 6473
Is your source file size divisible by 4? If not, then any remaining bytes will be ignored at the end of the operation. The i += 4 is going to skip over them. You'll need to handle those at the end, after your for loop, if the source (fits) file isn't perfectly divisible by 4.
Upvotes: 1