How to convert numbers into characters other than in the range 32-127?

2 次查看(过去 30 天)
I have the following code:
A = [10, 23, 90, 125, 145, 250];
B = char(A) % Converts double into character
C = ' Z}ú'; % 1x6 char (value of B) copied from 'Workspace'
D = double(C) % converts char into double
%% Conclusion: A and D are not same (why?)
kjkMoreoverM

回答(1 个)

dpb
dpb 2020-4-7
编辑:dpb 2020-4-7
Because
>> A = [10, 23, 90, 125, 145, 250];
B = char(A)
B =
'
Z}ú'
>> double('
Z}ú')
double('
Error: Character vector is not terminated properly.
>>
isn't at all the same thing as
>> C = ' Z}ú';
>> D = double(C)
You had to convert the linefeed ASCII 10 that was displayed as newline action on the command window into a blank to store it into variable C and so when you convert that string to double you reflect that modification to the original data.
OTOH,
>> B = char(A);
>> all(double(B)==A)
ans =
logical
1
>>
which conclusively illustrates that what is displayed on the terminal screen isn't what is stored in memory. Certain non-printing characters have definite meanings in use to perform actions on the hardware; a char(10) is one of those.
  2 个评论
Stephen23
Stephen23 2020-4-7
+1 good point well made.
We get so used to handling vectors of characters that it is easy to overlook the fact that the control characters actually are intended to control particular behaviors of the printer/display and are certainly not intended to be printed verbatim (if that even makes any sense... or is it the sound of one hand clapping?).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by