how to convert string to array?

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 19 Mag 2015
Modificato: Stephen23 il 21 Mag 2015
It is not clear what is expected here.
Given that the requirement that the number [aabc] may "...be binary,hex,etc..", then presumably this number will be stored in a string (aka character array). Given that a character array is already an array of individual characters, then it already fulfills the requirements of the output: [a a b c], which is the same thing as [aabc], where each of a, b, etc are characters. It is possible to split the each character of the character array into a cell array, but what is the advantage of that?
Or is the question about something else?
After some processing, I got some value which is of the form abcd.this value can be either binary or hex.I have to split this bit by bit that is, a b c d.
wu chao
wu chao il 21 Mag 2015
When I want to split abcd,I usually use ch={a,b,c,d} instead of ch=[a b c d].Because ch=[a b c d] is the same thing as [abcd].
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

For the case of binary or decimal,
A - '0'
is enough. Or you can
sscanf(A, '%1d').'
For hex you can use
sscanf(A, '%1x').'
or you can use
Output = A - '0';
mask = Output>15;
Output(mask) = Output(mask) - 7;
@Walter Roberson could you please explain how A-'0' works?
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.

s = 'aabd';
C = regexp(s, '.', 'split');
or
C = num2cell(s);
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

Community Treasure Hunt

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

Start Hunting!

Translated by