how to convert string to array?
Show older comments
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 Comments
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?
Resmi Raveendran
on 21 May 2015
wu chao
on 21 May 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].
Guillaume
on 21 May 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).
Answers (3)
Keerthana B
on 19 Mar 2020
Edited: Walter Roberson
on 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 Comments
Walter Roberson
on 21 Mar 2020
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;
MD AMIMUL IHSAN
on 7 Feb 2022
@Walter Roberson could you please explain how A-'0' works?
Walter Roberson
on 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.
Walter Roberson
on 21 May 2015
s = 'aabd';
C = regexp(s, '.', 'split');
or
C = num2cell(s);
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!