How to get ascii value of characters stored in an array?
80 views (last 30 days)
Show older comments
I have a problem, where i need to get the ASCII values of characters that are stored in a character array. Please help me with sufficient details. Thanks in advance.
1 Comment
Anil Chowdary Tummala
on 16 Feb 2021
Edited: Walter Roberson
on 16 Feb 2021
I need to convert ASCII to string back in Matlab.
I am using this piece of code in Matlab
clc; close all;
my_string='Shift us up!';
%double(my_string);
ascii_codes=double(my_string);
reconstructed_string=native2unicode(ascii_codes,'');
but I should use this piece of code as program for Matlab Function Block in simulink where native2unicode is not supported giving the following errors
Function 'native2unicode' is not supported for code generation. Consider adding coder.extrinsic('native2unicode') at the top of the function to bypass code generation. Function 'MATLAB Function' (#23.31.56), line 4, column 5: "native2unicode(u,'ASCII')" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'string2ASCII/MATLAB Function'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'string2ASCII/MATLAB Function' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
what should I do
Answers (1)
Stephen23
on 3 Aug 2016
Edited: Stephen23
on 16 Feb 2021
>> str = 'Hello World';
>> double(str)
ans =
72 101 108 108 111 32 87 111 114 108 100
Or use uint32 for the entire range of characters supported by the MATLAB char type (fixed thank you @Guillaume).
9 Comments
Guillaume
on 4 May 2020
%note that I made a mistake in my comment I said to use unicode2native in the text, which is correct
%but wrote native2unicode in the example which is incorrect.
codes = unicode2native(yourstring, 'US-ASCII');
will give you the ASCII character codes of the characters of yourstring which are valid ASCII characters, so you'll get numbers between 0-127.
Parity bits have nothing to do with ASCII codes. Parity bits are normally associated with a transmission protocol (which may indeed use ASCII to encode characters).
It's unclear what you mean by 'binary code'. As said you'll get numbers between 0-127. If you want to convert that to a 'binary' representation you have to do that yourself.
Rik
on 16 Feb 2021
Edited: Rik
on 16 Feb 2021
Just a clarification: Matlab uses UTF-16 to encode chars (this is not undocumented, but fairly hidden), so double(char_array) will not work correctly for all characters. The attached function implements the conversion from UTF-16 to UTF-32 (i.e. to the Unicode code points).
str = 'Hi 😱🙂🤿';
double(str) % incorrect result
UTF16_to_unicode(str)
See Also
Categories
Find more on Data Type Conversion 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!