logo for matrixlab-examples.com
leftimage for matrixlab-examples.com

ASCII Chart in Matlab

In this article we have three goals: first, we’re going to develop an ASCII chart to understand instructions char and double in Matlab; second, we’re going to work with a simple ASCII conversion technique as an example of simple encoding, and third, we’re going to develop a rot13 method, which happens to be another encoding technique.

ASCII Character Set

ASCII stands for American Standard Code for Information Interchange. Computers can only understand binary numbers, so an ASCII code is the internal representation of a character such as 'z' or '4'. Matlab has two useful functions to work with them: char and double.

S = char(X) converts the array X that contains positive integers     representing character codes into a Matlab

character array (the first 127 codes are ASCII). The actual characters displayed depend on the character set encoding for a given font.  The result for any elements of X outside the range from 0 to 65535 is not defined (and may vary from platform to platform). 

double(S) returns the double precision value for S.

 
We can create a printable ASCII chart or table in Matlab, like this:

for i = 32 : 63
    str = [num2str(i)
' ' char(i) '     '...
           num2str(i+32) ' ' char(i+32) '     '...
           num2str(i+64) ' ' char(i+64)];
    disp(str)

end

 
The resulting ASCII chart or table of equivalencies is:
 

32       64 @     96 `
33 !     65 A     97 a
34 "     66 B     98 b
35 #     67 C     99 c
36 $     68 D     100 d
37 %     69 E     101 e
38 &     70 F     102 f
39 '     71 G     103 g
40 (     72 H     104 h
41 )     73 I     105 i
42 *     74 J     106 j
43 +     75 K     107 k
44 ,     76 L     108 l
45 -     77 M     109 m
46 .     78 N     110 n
47 /     79 O     111 o
48 0     80 P     112 p
49 1     81 Q     113 q
50 2     82 R     114 r
51 3     83 S     115 s
52 4     84 T     116 t
53 5     85 U     117 u
54 6     86 V     118 v
55 7     87 W     119 w
56 8     88 X     120 x
57 9     89 Y     121 y
58 :     90 Z     122 z
59 ;     91 [     123 {
60 <     92 \     124 |
61 =     93 ]     125 }
62 >     94 ^     126 ~
63 ?     95 _     127 

 

ASCII Conversion / Encoding

ASCII codes are useful in the interchange of information between electronic devices, and they could be encrypted in some cases. Let’s play a little game... Let’s say that the IT department of Special International Decoders, Inc. is trying to decode intercepted messages. Senior decoders have determined that a particular company  encodes messages by first converting all characters to their ASCII values and then reversing the string.

For example, consider ‘C3’. The ASCII values are 67 and 51, respectively. 
Then reverse this to get string 15 76 as the coded message.
 

We’re going to write a program which reads a coded message and decodes it. We’re going to encode messages, too! 

Let’s say that first we want to encode the sentence “Hello World”. We can do this:
 

% Initial message is a string
m = 'Hello World';
% Find the ASCII equivalent
n = double(m);
% Convert to string and flip it
encoded = fliplr(num2str(n))

 

The resulting string is
 

encoded =
001  801  411  111  78   23   111  801  801  101  27

Now, the second part:

% Take the encoded message and flip it
cf = fliplr(encoded);
% Find ASCII equivalents
n = str2num(cf);
% Convert to ASCII
decoded = char(n)


As an experiment on your own, try to decode this message...

911  111  411  411  111  901  111  611  23   611  111  011  23   44   121  79   001  111  611  23   511  611  411  79   611  511  23   101  411  711  611  711  201  23   101  401  611

What do you get? Remember, you have to enter a string as input, not a numerical vector. So you’ll use your decoding routine with something like encoded = ‘...’


ROT13

ROT-13 is an encoding easily decodable. It’s often used on usenet groups when someone wants to post a text that should not be retrieved through search engines, or for posting things that might offend some readers, or spoilers. It replaces each English letter with the one 13 places forward or back along the alphabet. A major advantage of rot13 is that it is self-inverse, so the same code can be used for encoding and decoding.

 
This is a possible approach in Matlab:

% Place your string here
m = 'your message here'
% Find indices of letters that need a +13
ix1 = find(65 <= m & m <= 77 | 97 <= m & m <= 109);
% Find indices of letters that need a -13
ix2 = find(78 <= m & m <= 90 | 110 <= m & m <= 122); 

% Replace letters
m(ix1) = m(ix1) + 13;
m(ix2) = m(ix2) - 13; 

% Display encoding
encoded = char(m) 


The result in this case is:

encoded =
lbhe zrffntr urer


 From 'ASCII Chart' to home

 From 'ASCII Chart' to Fun!

Top

Concatenate Strings

Palindromes




footer for matlab page