how to convert string to array?

54 visualizzazioni (ultimi 30 giorni)
Resmi Raveendran
Resmi Raveendran il 19 Mag 2015
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 Commenti
Stephen23
Stephen23 il 21 Mag 2015
Modificato: Stephen23 il 21 Mag 2015
@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 il 21 Mag 2015
@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).

Accedi per commentare.

Risposte (3)

Keerthana B
Keerthana B il 19 Mar 2020
Modificato: Walter Roberson il 21 Mar 2020
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 Commenti
MD AMIMUL IHSAN
MD AMIMUL IHSAN il 7 Feb 2022
@Walter Roberson could you please explain how A-'0' works?
Walter Roberson
Walter Roberson il 8 Feb 2022
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.

Accedi per commentare.


Walter Roberson
Walter Roberson il 21 Mag 2015
s = 'aabd';
C = regexp(s, '.', 'split');
or
C = num2cell(s);

Azzi Abdelmalek
Azzi Abdelmalek il 19 Mag 2015
a=1234
b=num2str(a)-'0'
  1 Commento
Jan
Jan il 19 Mag 2015
Modificato: Jan il 19 Mag 2015
This does not work for hex numbers.

Accedi per commentare.

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by