Converting 27 bit signed binary to decimal

25 ビュー (過去 30 日間)
Karl Haebler
Karl Haebler 2018 年 1 月 5 日
コメント済み: Walter Roberson 2018 年 1 月 5 日
I have a textfile with 1024 lines, each line containing a 27 bit signed binary number. I need to convert the data in this textfile to decimal. I know how to convert signed binary of length 16, but I'm not sure how to convert 27 bit since the function uint16 does not apply to 27 bit numbers. Does anyone know how to convert 27 bit signed binary numbers into decimal? Thank you very much
  2 件のコメント
James Tursa
James Tursa 2018 年 1 月 5 日
What exactly is in the text file? Can you give a short example? Is it character data with '0' and '1', or something else?
Karl Haebler
Karl Haebler 2018 年 1 月 5 日
Hey James, I've attached the textfile here

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 1 月 5 日
Your input file does not have 27 bit signed binary numbers: it has 32 bit signed binary numbers. 22 bits of the range are significant, but because they are stored as 32 bit you can read them as 32 bit.
fid = fopen('output_real.txt', 'rt');
datacell = textscan(fid, '%s');
fclose(fid);
values = typecast(uint32(bin2dec(char(datacell{1}))),'int32');
  2 件のコメント
Karl Haebler
Karl Haebler 2018 年 1 月 5 日
Hey Walter, You are right, I actually uploaded the text file after I adjusted it so that I could use uint32. So basically originally the textfile did have 27 bit signed numbers, then I recreated it to have 32 so I could use the method you showed. I'm still curious though, do you know how you would do the conversion if it did in fact just have 27 bit numbers? Thanks!
Walter Roberson
Walter Roberson 2018 年 1 月 5 日
fid = fopen('output_really_real_not_that_fake_32bit_stuff.txt', 'rt');
datacell = textscan(fid, '%s');
fclose(fid);
temp = char(datacell{1}));
temp = [repmat(temp(:,1),1,5), temp]; %sign extend
values = typecast(uint32(bin2dec(temp)), 'int32');

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by