How to convert numbers into characters other than in the range 32-127?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (1)
dpb
il 7 Apr 2020
Modificato: dpb
il 7 Apr 2020
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 Commenti
Stephen23
il 7 Apr 2020
+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?).
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!