how to convert string to array?

54 vues (au cours des 30 derniers jours)
Resmi Raveendran
Resmi Raveendran le 19 Mai 2015
Commenté : Walter Roberson le 8 Fév 2022
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 commentaires
Stephen23
Stephen23 le 21 Mai 2015
Modifié(e) : Stephen23 le 21 Mai 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 le 21 Mai 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).

Connectez-vous pour commenter.

Réponses (3)

Keerthana B
Keerthana B le 19 Mar 2020
Modifié(e) : Walter Roberson le 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 commentaires
MD AMIMUL IHSAN
MD AMIMUL IHSAN le 7 Fév 2022
@Walter Roberson could you please explain how A-'0' works?
Walter Roberson
Walter Roberson le 8 Fév 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.

Connectez-vous pour commenter.


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

Azzi Abdelmalek
Azzi Abdelmalek le 19 Mai 2015
a=1234
b=num2str(a)-'0'
  1 commentaire
Jan
Jan le 19 Mai 2015
Modifié(e) : Jan le 19 Mai 2015
This does not work for hex numbers.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by