C#十六进制模式扫描 [英] C# hex pattern scanning

查看:127
本文介绍了C#十六进制模式扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#扫描图片文件的特定模式?

我能够使用binaryReader扫描文件的标题和预告片。

当提取标题时,我将它与本地数据库中的标题代码进行比较,它将返回指定的文件扩展名。



现在我想要扫描图片文件的十六进制代码的特定模式。

示例:'41 70 70 6c 65'将指示图片的来源来自Apple Iphone。



how to i scan for a particular 'pattern' of a picture file using c#?
I am able to scan for the header and trailer of the file using the binaryReader.
when the header has been extracted, I will compare it with the header code in the local database, and it will return the designated file extension.

Now i would like to scan for the particular pattern of the picture file's hex code.
example: '41 70 70 6c 65' will indicating the source of the picture is from an Apple Iphone.

private void sourceDatabase(BinaryReader reader)
        {
            byte[] buffer = new byte[5];
            reader.BaseStream.Seek(134, 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 > 6)
                {
                    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;
            }
        }

推荐答案

我不知道是什么让你觉得你可以找出一些来源捕获一些特定模式的图像。我不这么认为。当然,表达十六进制模式是荒谬的:十进制或十六进制只是使用一个或另一个基数的位置表示法中某些数字数据的字符串表示的工件;它们都不存在于文件数据中,它总是二进制的。我也无法相信十六进制可以帮助您识别图像的来源。也许你在谈论图像文件的元数据部分的一些特征部分,但它与模式无关。



无论如何,如果你需要捕获图像本身的位而不是元数据,那么你做错了。你从文件开头的134位开始,但谁告诉你这是图片部分的起始位置?如果你真的需要图像位,你可以使用,例如, System.Drawing.Bitmap System.Drawing.Bitmap.LockBits

https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com /en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx [ ^ ]。



-SA
I have no idea what makes you thinking that you can identify the source of some image by capturing some specific pattern. I don't think so. And of course the expression "hex pattern" is absurd: "decimal" or "hexadecimal" is just the artifact of string representation of some numeric data in positional notation using one or another base; none of them really exist in file data, which is always binary. I also cannot believe that hexadecimal can help you to identify the source of the image. Maybe you are talking about some signature part of the metadata portion of the image file, but then it has nothing to do with "patterns".

Anyway, if you need to capture the bits of the image itself, not metadata, you are doing it wrong. You start from the position 134 of the beginning of the file, but who told you that this is where the image part starts? If you really need to image bits, you can use, for example, System.Drawing.Bitmap and System.Drawing.Bitmap.LockBits:
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx[^].

—SA


这篇关于C#十六进制模式扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆