Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 将二进制文件分组,并在8个数字后添加空格(重复)_String_Matlab_Matrix_Binary_Mp3 - Fatal编程技术网

String 将二进制文件分组,并在8个数字后添加空格(重复)

String 将二进制文件分组,并在8个数字后添加空格(重复),string,matlab,matrix,binary,mp3,String,Matlab,Matrix,Binary,Mp3,我希望将8个二进制数组合在一起,然后将其形成矩阵,如图所示: 01001001 10110100 10111101 10000111 10110101 10100011 10110010 10111000 010 我的输入将是一个MP3文件 我设法在8个二进制文件后添加一个空格,但不知道如何在4组8个二进制文件后将其添加到新行 我的Matlab编程是: fid=fopen('mp3file.mp3','r','b'); x=uint8(fread(fid,'ubit1')); a

我希望将8个二进制数组合在一起,然后将其形成矩阵,如图所示:

01001001  10110100  10111101  10000111
10110101  10100011  10110010  10111000
010
我的输入将是一个MP3文件

我设法在8个二进制文件后添加一个空格,但不知道如何在4组8个二进制文件后将其添加到新行

我的Matlab编程是:

fid=fopen('mp3file.mp3','r','b');
x=uint8(fread(fid,'ubit1'));
a = 1;

if ( a+100 <= numel(x) )
    B = x(a:a+25);
    str = [repmat('%d', 1, 8) ' '];
    fprintf(str,B);
end
我发现有与此类似的帖子,但其中一篇只适用于字符/字母表,而不适用于二进制:


有什么想法吗?

正则表达式可能是一种很好的方法,但我更喜欢重塑,所以这里有一个解决方案

此外,我不确定您读取文件的方式—您一点一点地读取,但随后您将其转换为uint8。为什么不直接读uint8呢

这样的顺序是:读取文件中的所有字节UTIT8,将其转换为字符,然后将其改写为指定的列数,并在中间添加空白。 它说:

%% // read the file byte by byte (8 bits at a time)
fid = fopen('dice.png','r','b');
x = fread(fid,'*uint8') ;
fclose(fid);                        %// don't forget to close your file

%% //  pad the end with 0 if we don't have a multiple of nByteColumn
nByteColumn = 4 ; %// your number of columns

nByte     = length(x) ;                             %// total number of byte read from the file
nByte2pad = nByteColumn-mod(nByte,nByteColumn) ;    %// number of byte to add to get a full array
x = [x ; zeros( nByte2pad , 1 , 'uint8' ) ];        %// do the padding

%% // now convert to alphanumeric binary (=string) and reshape to your liking
S = [repmat(' ',nByte+nByte2pad,1) dec2bin(x)].' ; %'// convert to char and add a whitespace in front of each byte/string
S = reshape( S , nByteColumn*9 , [] ).' ;          %'// reshape according to your defined number of column 
S(:,1) = [] ;                                      %// remove the leading whitespace column
您将获得一个字符数组,其中包含按指定列数成形的所有值:

>> S(1:3,:)
ans =
10001001 01010000 01001110 01000111
00001101 00001010 00011010 00001010
00000000 00000000 00000000 00001101
假设x包含数据,使得数组的每个元素都是数字0或1,则此代码执行以下操作:

xs = char('0' + x);
xs = [xs , repmat(' ', 1, 8 - mod(numel(x), 8))];
xs = reshape(xs(:), 8, [])';
xg = cellstr(xs);
fprintf('%s %s %s %s\n', xg{:})
fprintf('\n')
逐行评论:

如Luis Mendo a.k.a string所指出的,将整数数组转换为字符数组:

xs = char('0' + x);
这使用ASCII算法:符号“0”和“1”在ASCII代码表中按顺序排列

用空格填充字符串,使其为8的倍数:

xs = [xs , repmat(' ', 1, 8 - mod(numel(x), 8))];
这里8-modnumelx,8计算使字符串长度为8的倍数所需的空格数

使用“重塑”形成8位数字组:

xs = reshape(xs(:), 8, [])';
在此之后,xs是一个二维字符数组,每行有一个8位数字组

使用fprintf打印:


有必要通过cellstr转换为单元格数组,以便fprintf可以作为单独的参数输入每一行,每个参数与格式字符串中的%s中的一个匹配。额外的换行是必要的,因为可能是最后一行输出不完整。

最终您将数字转换为字符串。所以这个问题可能会给你一些关于如何重塑你的最后一根弦的灵感。@Hoki,我试过你提到的方法。我的问题是,numeloutput=1。我的输出数组无法计数。结果,出现了错误:未定义“uint8”类型输入参数的函数“regexprep”…您需要首先将输入转换为char!试试像x=charx+'0'这样的东西;在应用regexprep之前。@LuisMendo,我知道了。衷心感谢您的帮助:-您的问题完全解决了吗?我们可以把这个问题作为Hoki链接的问题的副本来结束吗?
xs = reshape(xs(:), 8, [])';
xg = cellstr(xs);
fprintf('%s %s %s %s\n', xg{:})
fprintf('\n')