我在matlab中将H.264比特流读作Hex文件。我想在满足某些条件时插入一些字符串。
如同在附加图像中,如果文件中任何地方出现十六进制值00 00 00 01,我想添加一些像ABC在00 00 00 01之前的文件中。字符串比较很容易,但如何进行十六进制比较?
这是我将文件读取为hex文件的代码
f = fopen(theFile);
if f==-1
return
end
c = fread(f);
theSize=prod(size((c)));
c=sprintf('%02x\n',c);
c(3:3:end)='';
m=floor(length(c)/nChars);
hex='';
hex=reshape(c(1:m*nChars),nChars,m)';
if mod(length(c),nChars)
hex=strvcat(hex,c(m*nChars+1:end));
end
更具体地说,我希望将此c代码转换为matlab
QByteArray data, basePattern;
basePattern.resize(3);
//start code:
basePattern[0] = (char) 0x00;
basePattern[1] = (char) 0x00;
basePattern[2] = (char) 0x01;
char end1 = 0x25, end2 = 0x45, end3 = 0x65;
x = myfile;//read using fopen
if (x == end1 || x == end2 {
}
答案 0 :(得分:1)
十六进制值实际上只是整数:
x = uint8(hex2dec({'01', '02', '0A', '0B', '25', '45', '65', '00', '01', 'AA'}))
x =
1
2
10
11
37
69
101
0
1
170
可以直接比较它们:
x(3) == uint8(hex2dec('0a'))
ans =
1
所以把它们放在一起,你应该创建一个新的缓冲区并搜索模式的字节,如果找到它,插入数据,如果没有找到,只需附加字节:
pat0 = uint8(hex2dec('00'));
pat1 = uint8(hex2dec('00'));
pat2 = uint8(hex2dec('01'));
pos = 1;
data = % the uint8 array read in from the file.
new_data = uint8([]);
while pos < length(data) - 2
if data(pos+0) == pat0 && data(pos+1) == pat1 && data(pos+2) == pat2
% insert new data buffer and append pattern
new_data = [new_data my_data_to_insert pat0 pat1 pat2];
pos = pos + 3;
else
% append
new_data = [new_data data(pos)];
pos = pos + 1;
end
end
% append last 2 bytes
new_data = [new_data data(end-1:end)];