how to convert string to array?

조회 수: 296 (최근 30일)
Resmi Raveendran
Resmi Raveendran 2015년 5월 19일
댓글: Walter Roberson 2022년 2월 8일
How can I convert a number say [aabc] to an array [a a b c]?.These numbers may be binary,hex,etc.. The value i got after some calculations is in the form of abcd where each bits can be either binary or hex.I have to separate it bit by bit as a b c d
  댓글 수: 5
Stephen23
Stephen23 2015년 5월 21일
편집: Stephen23 2015년 5월 21일
@Resmi Raveendran: Can you please provide exact examples of what you have and what you need, not just abstract abcd representations. Show us a samples of this value, and the desired outputs.
Guillaume
Guillaume 2015년 5월 21일
@Remi, I agree your question is not clear. In particularly, the "split this bit by bit". What does that mean for an hexadecimal string where each character represents 4 bits.
Do you mean convert a string representation to binary?
Also what indicates that a string is a hexadecimal number or a binary number? The hexadecimal '1010' is a very different number (= 4412 in decimal) from the binary '1010' (= 10 in decimal).

댓글을 달려면 로그인하십시오.

답변 (3개)

Keerthana B
Keerthana B 2020년 3월 19일
편집: Walter Roberson 2020년 3월 21일
A='01010001101010';
Output=char(num2cell(A));
Output=reshape(str2num(Output),1,[])
The output will be a 1×n array of the the string A
  댓글 수: 3
MD AMIMUL IHSAN
MD AMIMUL IHSAN 2022년 2월 7일
@Walter Roberson could you please explain how A-'0' works?
Walter Roberson
Walter Roberson 2022년 2월 8일
Each character is internally coded as a 16 bit integer (this might not be absolutely completely true, but is hard to prove otherwise.)
There are tables, from the Unicode Consortium, of which integer corresponds to which character (or effector or accent mark, or related purpose.) It in an international standard.
For most purposes, it does not matter which exact integer that any particular character encodes to, as long as everyone is consistent about it. Does 'X' encode to 88 or 120? Doesn't really matter if everyone agrees on the value.
However, the particular integers that were chosen have several relationships:
  • all of the encodings for the digits '0' to '9' are consecutive values -- the code for '2' is exactly 2 greater than the code for '0'
  • all of the encodings for the English letters 'A' to 'Z' are consecutive values -- the code for 'C' is exactly 2 greater than the code for 'A'
  • all of the encodings for the English letters 'a' to 'z' are consecutive values -- the code for 'c' is exactly 2 greater than the code for 'a'
There are random characters between the numeric and capitals and lower-case ranges. There are some convenient numeric relationships for them, but for anything other than high efficiency code, the exact relationships do not matter.
But because the codes for each range are consecutive, to find out the relative numeric value of any particular digit character such as '7', you can subtract the character code for the digit '0' from the code in question, like ('7' - '0') will be the numeric value 7. Likewise, if you are interested in (say) the 22nd character of the upper-case alphabet then it is the one 22 into the list. 'A'+22-1 --> 'V' (remember that 'A' is the first character, that's why the -1)
So you can convert a list of binary characters '0' and '1' to the corresponding numeric offsets 0 and 1, by subtracting the character '0' -- '0'-'0' is of course 0, and because of previous arrangement from the standards, '1'-'0' is 1.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2015년 5월 21일
s = 'aabd';
C = regexp(s, '.', 'split');
or
C = num2cell(s);

Azzi Abdelmalek
Azzi Abdelmalek 2015년 5월 19일
a=1234
b=num2str(a)-'0'
  댓글 수: 1
Jan
Jan 2015년 5월 19일
편집: Jan 2015년 5월 19일
This does not work for hex numbers.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by