I have a .dat file with formatted in binary. This .dat file contains a blacklisted value with presented ones as true and zeroes as false. When opened with notepad or notepad++, I can't see the zeroes and ones inside. Only the strange character shows.
Example :
ù¿.ÿôý½¯ÿËûþÿÅ}ó
But I have download the hex editor, with that software I can see the hex value. The value is
F9 BF 7F FF F4 FD BD AF FF CB FB FE FF C5 7D F3
For each hex value, I need to change in binary format. So let's say for F9, when we change to binary it will become 11111001 and for BF it will be 10111111 and so on.
So in the binary value, I need to reverse on every binary digit then save it to database.Let say current value is 11111001, then I will got 10011111 and every binary digit will store to database. Same as 10111111 and so on. So in database table, for F9 it will become
id flag_blacklist
1 1
2 0
3 0
4 1
5 1
6 1
7 1
8 1
And continue for value BF it will becomes
id flag_blacklist
9 1
10 1
11 1
12 1
13 1
14 1
15 0
16 1
Until finish read all the contains in .dat file.
The flow is Hex value > Binary Value > Reverse Binary Digit > Store to database
After Google a few hours, I still not found the best answer and suited with my case. If have any link can help me to solved this problems or required more informations, please let me knows.
The maximum size of .dat files is 245 KB.
It sounds like you just want to read the file as a byte array and reverse the bit order for each byte.
byte[] data = File.ReadAllBytes("file.dat");
for (int i = 0; i < data.Length; i++)
{
data[i] = ReverseBits(data[i]);
}
// Now write data to the database, however you want to.
where the ReverseBits
method can be be written in a "naive but reasonably easy to understand" way as:
private static byte ReverseBits(byte value)
{
// Just mask out each bit in turn and work out where it should end up.
return (byte) (((value & 0x01) << 7) |
((value & 0x02) << 5) |
((value & 0x04) << 3) |
((value & 0x08) << 1) |
((value & 0x10) >> 1) |
((value & 0x20) >> 3) |
((value & 0x40) >> 5) |
((value & 0x80) >> 7)));
}
... or you could use that naive way to build a 256-element lookup table, then use that when you're processing the bytes.
See more on this question at Stackoverflow