简体   繁体   English

从.dat文件读取二进制数据并反转二进制值C#的最佳方法

[英]The best way to read binary data from .dat file and reverse a binary value C#

I have a .dat file with formatted in binary. 我有一个.dat文件,其格式为二进制。 This .dat file contains a blacklisted value with presented ones as true and zeroes as false. 该.dat文件包含一个列入黑名单的值,其中呈现的值为true,零表示为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 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. 因此,对于F9来说 ,当我们更改为binary时,它将变为11111001 ,对于BF ,它将为10111111 ,依此类推。

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. 所以在二进制值中,我需要反转每个二进制数字然后将其保存到数据库中。假设当前值为11111001 ,那么我将得到10011111并且每个二进制数字都将存储到数据库中。 Same as 10111111 and so on. 10111111相同,依此类推。 So in database table, for F9 it will become 因此在数据库表中,对于F9 ,它将变为

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 并继续求值BF ,它将变为

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. 直到读完.dat文件中的所有内容。

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. 在Google待了几个小时之后,我仍然找不到最佳答案,也无法满足我的情况。 If have any link can help me to solved this problems or required more informations, please let me knows. 如果有任何链接可以帮助我解决此问题或需要更多信息,请让我知道。

Edited : 编辑:

The maximum size of .dat files is 245 KB. .dat文件的最大大小为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: ReverseBits方法可以以“天真但相当容易理解”的方式编写为:

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. ...或者您可以使用这种幼稚的方法来构建256个元素的查找表,然后在处理字节时使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM