Matlab-如何一次读取2个字节 [英] Matlab - how to read 2 bytes at a time

查看:730
本文介绍了Matlab-如何一次读取2个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数字-778310098-我想一次读取2个字节.因此,我期望我的输出为77; 83; 10; 09; 8.我尝试使用以下方法: uint16(fread(fileID,inf,'ubit8')),我得到的输出是各个数字的ASCII值: 55 55 56 51 49 48 48 57 56

I have a number like this - 778310098 - and I want to read 2 bytes at a time. So, I am expecting my output to be 77; 83; 10; 09; 8. I tried using the below: uint16(fread(fileID,inf, 'ubit8')) and the output I get is the ASCII value of the individual numbers: 55 55 56 51 49 48 48 57 56

我需要怎么做才能获得所需的输出?

What do I need to do to get the desired output?

推荐答案

一种方法是将其转换为字符串,然后处理该字符串,然后将其转换回整数.尽管这可能不是特别优雅或完美,但这会成功吗?

One method is to convert it to a string, then process the string, then convert it back to an integer. While this may not be particularly elegant or perfect, will this do the trick?

a = 778310098;
b = num2str(a);

for i = 1:2:length(b)
    if i == length(b)                 % to handle the case for odd input
        split = str2num(b(i))
    else
        split = str2num(b(i:i+1))     % handle all others
    end
end

这篇关于Matlab-如何一次读取2个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆