Matlab中的十六进制数据处理

时间:2014-07-23 03:56:49

标签: matlab type-conversion decimal hex

我有一串Hex数据流作为我的数据,它看起来像这样: " 00 E1 08 A2 05 FF"

我需要将它们连接起来,然后以列格式转换为十进制,如下所示:

00E1

08A2

05FF

最后转换为十进制后:

225

2210

1535

可用的总数据为70,000。

任何人都可以帮助我吗?我是Matlab的新手,我尝试编写以下代码,但没有运气。

N=140000; %N is the number of data
n=N/2; %n is the number of concatenated data
filename=input('\nFile-name: ','s');
data_import=importdata(filename,' ', 1);

data_char=char(data_import);
data_str=sscanf(data_char, '%s', [N,1]);
data_out=textscan(data_str, '%4c',n);

2 个答案:

答案 0 :(得分:0)

基于{p> importdata的方法 -

data_import = importdata(filename,'')
C = regexp(data_import,' ','Split')
C1 = C{:}
t1 = reshape(C1(~cellfun(@isempty,C1)),2,[]).' %//'
data_out = hex2dec([char(t1(:,1)) char(t1(:,2))])

基于替代textscan的方法 -

fileID = fopen(filename);
data_import = textscan(fileID,'%s')
fclose(fileID);

t1 = reshape(data_import{:}.',2,[]).'
data_out = hex2dec([char(t1(:,1)) char(t1(:,2))])

答案 1 :(得分:0)

filename=input('\nFile-name: ','s');
data_import=importdata(filename,' ', 1);
data_char=char(data_import);
data_str=sscanf(data_char, '%s', [N,1]);
data_out = [];
for i=1:4:N
   a = data_str(i:i+4);
   d = hex2dec(a);
   data_out = [data_out;d];
end

希望这会有所帮助..

编辑:

示例字符串:00 D5 00 CE 00 C7 00 C0 00 B9 00 D5 00 CE 00 C7 00 C0 00 B9 00 D5 00 CE 00 C7 00 C0 00 B9

代码:

data = '00 D5 00 CE 00 C7 00 C0 00 B9 00 D5 00 CE 00 C7 00 C0 00 B9 00 D5 00 CE 00 C7 00 C0 00 B9';
a = regexprep(num2str(data,17), '\s', '');
sizeOfA = size(a,2);
data_out = [];
for i = 1:4:sizeOfA
    a1 = a(i:i+3);
    d = hex2dec(a1);
    data_out = [data_out;d];
end