フィルターのクリア

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

2 ビュー (過去 30 日間)
Abdul Gaffar
Abdul Gaffar 2020 年 4 月 7 日
コメント済み: dpb 2020 年 4 月 7 日
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?).
dpb
dpb 2020 年 4 月 7 日

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by