C# 在特定位置读取十六进制并返回相应文本
选择图片文件后,程序将运行,通过与提供的数据库进行比较来扫描文件头 然后继续确定图片文件的文件源。问题是,当我使用系统扫描图片文件时,我无法像在页眉上那样找到源 检索具有特定格式的十六进制代码(例如:40 70 6c 65),将其与数据库进行比较,返回“源代码”C# 在特定位置读取十六进制并返回相应文本,c#,database,hex,extraction,C#,Database,Hex,Extraction,选择图片文件后,程序将运行,通过与提供的数据库进行比较来扫描文件头 然后继续确定图片文件的文件源。问题是,当我使用系统扫描图片文件时,我无法像在页眉上那样找到源 检索具有特定格式的十六进制代码(例如:40 70 6c 65),将其与数据库进行比较,返回“源代码” private void sourceDatabase(BinaryReader reader) { byte[] buffer = new byte[5]; reader.BaseStream.
private void sourceDatabase(BinaryReader reader)
{
byte[] buffer = new byte[5];
reader.BaseStream.Seek(reader.BaseStream.Length, SeekOrigin.Begin);
reader.Read(buffer, 0, 5);
string source = "";
foreach (var item in buffer)
{
string temp = "0";
if (item.ToString("X").Length == 1)
source += temp + item.ToString("X") + " ";
else
source += item.ToString("X") + " ";
}
if (source.Length != 0)
source = source.Substring(0, source.Length - 1);
MessageBox.Show("File Source : " + source, "Result");
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["databaseConnectionString"].ConnectionString);
connection.Open();
SqlCommand command = null;
string ori_source = source;
//int flag = 1;
while (true)
{
string sql = "SELECT source FROM sourceDatabase WHERE Hexadecimal like '" + source + "%'";
command = new SqlCommand(sql, connection);
if (source.Length != 0 && source.Length > 5)
{
source = source.Substring(0, source.Length - 1);
if (command.ExecuteScalar() != null)
{
string scr = System.Convert.ToString(command.ExecuteScalar().ToString());
//string desc = command.ExecuteScalar().ToString();
MessageBox.Show("Source : " + scr, "Result");
//flag = 2;
break;
}
}
else break;
}
}