Input [Hex] String then convert to binary from Hex

4 ビュー (過去 30 日間)
WARRIOR24
WARRIOR24 2020 年 12 月 15 日
編集済み: Rik 2020 年 12 月 15 日
How can I convert this string [0,1,2,3,4,5,6,7,8,9,0xA,0xB] binary?
Hex inputs are:
0xA = 10
0xB = 11
My Goal is to get one long consecutive binary output to look like this:
change it decimal, then to binary, then combine all binary values
'0000 0001 0010 00010'
but with no spaces and continous. Basically make it into a 32bit vector
'00000001001000010'
I have tried this code:
Array = [0,1,2,3,4,5,6,7,8,9,0xa,0xb];
reshape(dec2bin(Array),1,[])
reshape(dec2bin(Array,8),1,[])
I get this Error:
>> untitled4
Error: File: untitled4.m Line: 1 Column: 31
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
  1 件のコメント
Rik
Rik 2020 年 12 月 15 日
Array = [0,1,2,3,4,5,6,7,8,9,0xa,0xb];
reshape(dec2bin(Array),1,[])
ans = '000000001111000011110000001100110011010101010101'
reshape(dec2bin(Array,8),1,[])
ans = '000000000000000000000000000000000000000000000000000000001111000011110000001100110011010101010101'
As you can see, your code runs in R2020b. I just tested on my own copy of R2020a, and it works there as well.
Also a side note: Array is not a string, it is not even a char, it is a uint8 array (which dec2bin probably converts to double internally).

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

回答 (1 件)

Jan
Jan 2020 年 12 月 15 日
I guess, you are using an older version of Matlab, which does not allow to write hex numbers in the code directly. Then:
HexArray = {'0','1','2','3','4','5','6','7','8','9','a','b'};
DecArray = hex2dec(HexArray);
reshape(dec2bin(DecArray),1,[])
reshape(dec2bin(DecArray,8),1,[])
  2 件のコメント
Rik
Rik 2020 年 12 月 15 日
I thought that as well, but this OP actually did what many didn't: marking the release they use. As that is R2020a, the original code should work as well.
Rik
Rik 2020 年 12 月 15 日
編集済み: Rik 2020 年 12 月 15 日
It turns out from a mostly duplicate thread that the release is actually R2019a instead.
@Warrior, please don't make these kinds of mistakes. The release matters a lot in cases like this.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by