如何 判断一个十六进制文件是否包含有“00 00 01 FC”? 如何 判断一个十六进制文件是否包含有“00 00 01 FC”?(如何根据帧格式) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 每次读取4个字节,然后比对,可以比对流,也可以把她转为int,然后比对int 网上搜索c# kmp,改改就行了。 using System;using System.Text;using System.Windows.Forms;using System.IO;namespace fileContainTest{ public partial class TestForm : Form { string filePath = @"D:\VS2008\test\fileContainTest\file.txt"; public TestForm() { InitializeComponent(); } private void btnTest_Click(object sender, EventArgs e) { string strFile = readFile(filePath); if (strFile.Contains("00 00 01 FC")) MessageBox.Show("含有"); } private string readFile(string path) { StreamReader fileReader = new StreamReader(filePath); StringBuilder strBuildFile = new StringBuilder(); string fileLine=""; while (fileLine != null) { fileLine = fileReader.ReadLine(); strBuildFile.Append(fileLine); } return strBuildFile.ToString(); } }} string filePath = @"D:\VS2008\test\fileContainTest\file.mp4";string strFile = readFile(filePath);if (strFile.Contains("00 00 01 FC")){}这样不行的,你不需要转换成二进制数组然后再判断吗? 你先解释一下什么叫“十六进制文件”...文件就只有二进制的,内容是什么完全取决于reader... 就是串匹配算法...说白了字符串查找也是这个原理.lz自己google一下kmp算法. UltraEdit看视频文件是这样的:....89 67 00 00 01 FC 67 42 00 1E AB 40 B0 4B 20 00 00 00 01 68 CE 38 80....,然后以 00 00 01 FC为一帧的开始如何根据码流帧格式,查找获取帧头部,判断文件类型? bool bFound = false; int pos = -1; string strFilename = @"E:\Users\matrixchen\Videos\day3part1_2MB_ch9.wmv"; FileStream stream = File.Open(strFilename, FileMode.Open); byte b1 = 0x00; byte b2 = 0x00; byte b3 = 0x01; byte b4 = 0xFC; if (stream != null) { int baseLen = 102400; byte[] buff = new byte[baseLen + 3]; int nRead = stream.Read(buff, 0, buff.Length); int nBasePos = 0; while (nRead > 3 && !bFound) { for (int i = 0; i < nRead - 3; i++) { if (buff[i] == b1 && buff[i + 1] == b2 && buff[i + 2] == b3 && buff[i + 3] == b4) { bFound = true; pos = nBasePos + i; break; } } nBasePos += baseLen; stream.Seek(-3, SeekOrigin.Current); nRead = stream.Read(buff, 0, buff.Length); } stream.Close(); stream.Dispose(); }结果: bFound : 是否找到pos :位置 while (nRead > 3 && !bFound)有用吗? 应该改成if(nRead > 3 && !bFound)吧? 请问 vs2008有什么工具可以在程序运行的时候跟踪错误,写在日志里面? timer、timer,你让我头疼。。。 如何在一个Form下刷新另一个Form的listview 取得泛型List的上一条记录 c#中画图的问题! 好东东愿与大家共享 用xml文件保存配置数据,但是这些数据是经常被使用的,经常IO读xml文件的话是不是不好? 主从表中如何获得子表当前记录 C# WINFORM程序运行报错,有的计算机上运行不会出错 紧急求救!!!!(在线) 怎么获得局域网另外一台计算机的文件目录? C#如何根据码流帧格式,查找获取帧头部,判断文件类型?
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace fileContainTest
{
public partial class TestForm : Form
{
string filePath = @"D:\VS2008\test\fileContainTest\file.txt";
public TestForm()
{
InitializeComponent();
} private void btnTest_Click(object sender, EventArgs e)
{
string strFile = readFile(filePath);
if (strFile.Contains("00 00 01 FC"))
MessageBox.Show("含有");
} private string readFile(string path)
{
StreamReader fileReader = new StreamReader(filePath);
StringBuilder strBuildFile = new StringBuilder();
string fileLine="";
while (fileLine != null)
{
fileLine = fileReader.ReadLine();
strBuildFile.Append(fileLine);
}
return strBuildFile.ToString();
}
}
}
string strFile = readFile(filePath);
if (strFile.Contains("00 00 01 FC"))
{}
这样不行的,你不需要转换成二进制数组然后再判断吗?
说白了字符串查找也是这个原理.
lz自己google一下kmp算法.
如何根据码流帧格式,查找获取帧头部,判断文件类型?
bool bFound = false;
int pos = -1;
string strFilename = @"E:\Users\matrixchen\Videos\day3part1_2MB_ch9.wmv";
FileStream stream = File.Open(strFilename, FileMode.Open); byte b1 = 0x00;
byte b2 = 0x00;
byte b3 = 0x01;
byte b4 = 0xFC;
if (stream != null)
{
int baseLen = 102400;
byte[] buff = new byte[baseLen + 3];
int nRead = stream.Read(buff, 0, buff.Length);
int nBasePos = 0;
while (nRead > 3 && !bFound)
{
for (int i = 0; i < nRead - 3; i++)
{ if (buff[i] == b1 && buff[i + 1] == b2 && buff[i + 2] == b3 && buff[i + 3] == b4)
{
bFound = true;
pos = nBasePos + i;
break;
}
}
nBasePos += baseLen;
stream.Seek(-3, SeekOrigin.Current);
nRead = stream.Read(buff, 0, buff.Length);
} stream.Close();
stream.Dispose();
}
结果:
bFound : 是否找到
pos :位置
应该改成if(nRead > 3 && !bFound)吧?