How to get ascii value of characters stored in an array?
80 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Anil Chowdary Tummala
2021 年 2 月 16 日
編集済み: Walter Roberson
2021 年 2 月 16 日
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
回答 (1 件)
Stephen23
2016 年 8 月 3 日
編集済み: Stephen23
2021 年 2 月 16 日
>> 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 件のコメント
Guillaume
2020 年 5 月 4 日
%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
2021 年 2 月 16 日
編集済み: Rik
2021 年 2 月 16 日
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)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!