hexadecimal value with zeros but without spaces.

7 ビュー (過去 30 日間)
Maria Imdad
Maria Imdad 2020 年 12 月 8 日
コメント済み: Walter Roberson 2020 年 12 月 9 日
Hi,
I have an algorithm which is generating outputs in hexadeciaml formats. Its a loop with with different hexadecimal values after every iteration. My ultimate purpose is to ocnvert these hexa deciamal values into binaries and combine all those values as one binary string. Now the problem is that I get "ee 06 da 7b " hexa decimal values like this, and in order to its conversionin binary I have to remove these spaces in between but, as I try to remove the spaces, leading zeros are also removed. That part which is performing these conversions and some output is also added as an image. I will be greatful if you can help, as i ahve already tried so many methods available on internet.
% to get hexa decimal value and save it to variable str
str = sprintf(' %2x', w(i, :));
% to remove spaces in between
str= str(~isspace(str))
%hex to binary conversion
str=reshape(str,[],2);
str(all(str == ' ', 2), :) = [];
str=hex2dec(str);
disp(str);
% str = strrep(str, ' ', '')
disp(class(str));
% str = strtrim( str);
% disp(str);
str=dec2bin(str,4);

採用された回答

Walter Roberson
Walter Roberson 2020 年 12 月 8 日
w = [0x97 0x06 0x0a 0x04]
w = 1×4
151 6 10 4
i = 1;
reshape(dec2bin(w(i,:), 8).', [], 1).'
ans = '10010111000001100000101000000100'
  4 件のコメント
Maria Imdad
Maria Imdad 2020 年 12 月 8 日
after all these conversions i am getting my data like this as a cell
'00' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00' '00'
now I have to convert this data into vector. i am getting these two errors now.
  1. 'Key has to be a vector (not a cell array) with 16 elements.'
  2. Elements of key vector have to be bytes (0 <= key(i) <= 255).
can you help me convert my data into vector . I have already tried cell2mat
Walter Roberson
Walter Roberson 2020 年 12 月 9 日
key_bytes = uint8(bin2dec(reshape(bit_vector, 8, [])).';

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by