How to get ascii value of characters stored in an array?

81 Ansichten (letzte 30 Tage)
Adhi
Adhi am 3 Aug. 2016
Bearbeitet: Walter Roberson am 16 Feb. 2021
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 Kommentar
Anil Chowdary Tummala
Anil Chowdary Tummala am 16 Feb. 2021
Bearbeitet: Walter Roberson am 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 3 Aug. 2016
Bearbeitet: Stephen23 am 16 Feb. 2021
MATLAB makes this really easy: you can use the double function:
>> 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 Kommentare
Guillaume
Guillaume am 4 Mai 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
Rik am 16 Feb. 2021
Bearbeitet: Rik am 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
ans = 1×9
72 105 32 55357 56881 55357 56898 55358 56639
UTF16_to_unicode(str)
ans = 1×6
72 105 32 128561 128578 129343

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by