将十六进制转换为十进制并将结果合并到matlab中的一个单元格中
Converting Hex to Decimal and merging the result into one cell in matlab
我有以下十六进制值:
a = '55 D1 1F 81';
b = '9A D2 1F 81';
c = 'EF D3 1F 81';
d = '79 D4 1F 81';
我希望将它们转换为二进制,然后再转换为十进制。
我想要的结果如下所示。
我也写了代码。但是它没有给我所需的结果。
它将数据转换为二进制,但翻转了二进制值,然后弄乱了最终的十进制结果。
代码如下:
a = '55 D1 1F 81';
b = '9A D2 1F 81';
c = 'EF D3 1F 81';
d = '79 D4 1F 81';
s = {a;b;c;d};
%s = cellfun(@strsplit, s, 'UniformOutput', false);
s = regexp(s,'(\w)','match');
s = vertcat(s{:});
%s = fliplr(s);
% Iterate over each row
for rowNum = 1:size(s,1)
% To build up binary string from left to right
binaryString = [];
% Iterate over each column
for colNum = 1:size(s,2)
% Convert hex -> dec -> 8-bit binary word
outputBin = dec2bin(hex2dec(s{rowNum,end+1-colNum}), 4);
binaryString = [binaryString, outputBin];
% Save solution to output array:
outputBinary{rowNum, colNum} = binaryString;
outputDec(rowNum, colNum) = bin2dec(binaryString);
end
end
谁能帮我解决这个问题?
您可以使用以下功能:
function C = HDB(s) %Hex Dec Bin
s = regexp(s,'(\w)','match');
c1 = flip([s{:}].');
len = 1:length(c1);
hex = arrayfun(@(x) c1(1:x).',len,'UniformOutput',0)
C.hex = arrayfun(@(x) flip(hex{x}),len,'UniformOutput',0)
C.dec = arrayfun(@(x) hex2dec(C.hex{x}),len,'UniformOutput',0)
C.bin = arrayfun(@(x) dec2bin(C.dec{x}),len,'UniformOutput',0)
end
例如,对于数组 a
,您可以使用:
C = HDB(a)
输出:
hex =
{
[1,1] = 1
[1,2] = 81
[1,3] = F81
[1,4] = 1F81
[1,5] = 11F81
[1,6] = D11F81
[1,7] = 5D11F81
[1,8] = 55D11F81
}
dec =
{
[1,1] = 1
[1,2] = 129
[1,3] = 3969
[1,4] = 8065
[1,5] = 73601
[1,6] = 13705089
[1,7] = 97591169
[1,8] = 1.4398e+09
}
bin =
{
[1,1] = 1
[1,2] = 10000001
[1,3] = 111110000001
[1,4] = 1111110000001
[1,5] = 10001111110000001
[1,6] = 110100010001111110000001
[1,7] = 101110100010001111110000001
[1,8] = 1010101110100010001111110000001
}
我有以下十六进制值:
a = '55 D1 1F 81';
b = '9A D2 1F 81';
c = 'EF D3 1F 81';
d = '79 D4 1F 81';
我希望将它们转换为二进制,然后再转换为十进制。 我想要的结果如下所示。
我也写了代码。但是它没有给我所需的结果。
它将数据转换为二进制,但翻转了二进制值,然后弄乱了最终的十进制结果。
代码如下:
a = '55 D1 1F 81';
b = '9A D2 1F 81';
c = 'EF D3 1F 81';
d = '79 D4 1F 81';
s = {a;b;c;d};
%s = cellfun(@strsplit, s, 'UniformOutput', false);
s = regexp(s,'(\w)','match');
s = vertcat(s{:});
%s = fliplr(s);
% Iterate over each row
for rowNum = 1:size(s,1)
% To build up binary string from left to right
binaryString = [];
% Iterate over each column
for colNum = 1:size(s,2)
% Convert hex -> dec -> 8-bit binary word
outputBin = dec2bin(hex2dec(s{rowNum,end+1-colNum}), 4);
binaryString = [binaryString, outputBin];
% Save solution to output array:
outputBinary{rowNum, colNum} = binaryString;
outputDec(rowNum, colNum) = bin2dec(binaryString);
end
end
谁能帮我解决这个问题?
您可以使用以下功能:
function C = HDB(s) %Hex Dec Bin
s = regexp(s,'(\w)','match');
c1 = flip([s{:}].');
len = 1:length(c1);
hex = arrayfun(@(x) c1(1:x).',len,'UniformOutput',0)
C.hex = arrayfun(@(x) flip(hex{x}),len,'UniformOutput',0)
C.dec = arrayfun(@(x) hex2dec(C.hex{x}),len,'UniformOutput',0)
C.bin = arrayfun(@(x) dec2bin(C.dec{x}),len,'UniformOutput',0)
end
例如,对于数组 a
,您可以使用:
C = HDB(a)
输出:
hex =
{
[1,1] = 1
[1,2] = 81
[1,3] = F81
[1,4] = 1F81
[1,5] = 11F81
[1,6] = D11F81
[1,7] = 5D11F81
[1,8] = 55D11F81
}
dec =
{
[1,1] = 1
[1,2] = 129
[1,3] = 3969
[1,4] = 8065
[1,5] = 73601
[1,6] = 13705089
[1,7] = 97591169
[1,8] = 1.4398e+09
}
bin =
{
[1,1] = 1
[1,2] = 10000001
[1,3] = 111110000001
[1,4] = 1111110000001
[1,5] = 10001111110000001
[1,6] = 110100010001111110000001
[1,7] = 101110100010001111110000001
[1,8] = 1010101110100010001111110000001
}