Second, any lower case letters in the original message are converted to upper case and encrypted as an upper case letter. This simplification is so you don't have to differentiate between upper and lower case letters
So to encrypt the message First Bytes! using the cipherabove we go through character by character.
F is converted to K
i is converted to I then N
r is converted to R then W
s is converted to S then X
t is converted to T then Y
the space is removed
B is converted to G
y is converted to Y then D
t is converted to T then Y
e is converted to E then J
s is converted to S then X
! is removed
So the encrypted message is KNWXYGDYJX
To decrypt the message the receiver would need the above key.Any letter is decoded by going back 5 letters in the alphabet.
Use the code above to decode this message:
TQNANFNXSNSJ
In this part of the lab you know the message has been encodedwith a shift of 5.
Approach: Write a function that takes in one input, the encodedmessage. A possible header for the function would be:
function result = shift5(message)
Create a result the same length as the message. The result should be the same length as the incoming message. You can obtain the length of a string or array variable using the length message.
length(someVariable)
returns the length of someVariable
To create a place to store the result use the zeros function. This creates an array of the specified size
result = zeros(1,12)
would create an array of 12 zeros
result = zeros( 1, length(message) )
would create an array of zeros equal to the length of message.
Loop through every character in the message
For each element of the message subtract 5 from each character in the original message (more specifically the number representing that represents that character.)
The ASCII code for 'A' is 65. The ASCII code for 'Z' is 90. We are undoing a shift of 5 on an encoded character that is a 'Z' when get the following.
'Z' = 90
'Z' - 5 = 85
char(85) = 'U'
But when undoing an encoded 'A' there is a problem
'A' = 65
'A' - 5 = 60
char(60) = '<'
Shifting 'A' back by 5 should result in 'V' not '<'!
This means there is a decision to make. If the result of the shift is less than 65 then we must add 26 to the result before we get the character.
'A' = 65
'A' - 5 = 60
60 < 90
60 + 26 = 86
86 = 'V'
Excellent!
Place the character in the correct spot in the result. (Same as the loop control variable)
When all done convert the result back to chars. This can be done with the chars function.
result = chars(result)
would convert result from numbers to characters.
Encoded the algorithm described above yields the followingMatLab function:
function result = shift5(message)
% decodes a message encoded with a Caesar shift
% cipher of 5. Assumes message is all upper
% case letters.
len = length(message);
result = zeros(1,len);
temp = ' ';
for ch = 1:len
temp = message(ch) - 5;
if (temp < 65)
temp = temp + 26;
end
result(ch) = temp;
end
result = char(result);
This function should be in the work directory already. If notyou can download it from the First Bytes Web site.
www.cs.utexas.edu/users/scottm/FirstBytes/
The message for this lab is
'YMJWJSJAJWBNQQGJHTRUQJYJJVZFQNYDZSYNQBTRJSYMJRXJQAJXMJQUYTRFPJQFBXFSIJQJHYQFBRFPJWXXZXFSGFSYMTSD'
You can download this from the web at
You could open the above file in a web browser, copy it, thenpaste it into your MatLab command window.
Desktop Tools and Development Environment |
Opening and Running Files
Opening Files
You can open a file from the Current Directory browser and the file opens in the tool associated with that file type.
To open a file, select one or more files and perform one of the following actions:
- Press the Enter or Return key.
- Right-click and select Open from the context menu.
- Double-click the file(s).
The file opens in the appropriate tool. For example, the Editor/Debugger opens for M-files, and Simulink opens for model (.mdl
) files.
To open a file in the Editor/Debugger, no matter what type it is, select Open as Text from the context menu. One exception is P-files (.p
), which you cannot open.
To open a file using an external application, select Open Outside MATLAB from the context menu. For example, if you select myfile.doc
, Open Outside MATLAB opens myfile.doc
in Microsoft Word, assuming you have the .doc
file association configured to start Word.
You can also import data from a file. Select the file, right-click, and select Import Data from the context menu. The Import Wizard opens. See the Import Wizard documentation for instructions to import the data.
Function Alternative.Use the open
function to open a file in the tool appropriate for the file, given its file extension. Default behavior is provided for standard MATLAB file types. You can extend the interface to include other file types and to override the default behavior for the standard files. For name.ext
, open
performs the following actions.
File Type
| Extension
| Action
|
---|
Figure file
| fig
| Opens figure name.fig in a figure window.
|
HTML file
| html
| Opens HTML file name.html in the MATLAB Web browser.
|
M-file
| m
| Opens M-file name.m in the Editor/Debugger.
|
MAT-file
| mat
| Opens MAT-file name.mat in the Import Wizard.
|
Model
| mdl
| Opens model name.mdl in Simulink.
|
P-file
| p
| Cannot open P-files (pseudocode files that do not expose the M-file code.
|
PDF file
| pdf
| Opens the PDF file name.pdf in the installed PDF reader, for example, Adobe Acrobat.
|
Variable
| none
| Opens the numeric or string array name in the Array Editor; open calls openvar .
|
Other
| custom
| Opens name.custom by calling the helper function opencustom , where opencustom is a user-defined function.
|
Use winopen
to open a file using an external application on Windows platforms.
To view the content of an ASCII file, such as an M-file, use the type
function. For example
displays the contents of the file startup.m
in the Command Window.
Running M-Files
To run an M-file from the Current Directory browser, select it, right-click, and select Run from the context menu. The results appear in the Command Window.
Creating, Renaming, Copying, and Removing Directories and Files | Finding Files and Content Within Files |
© 1994-2005 The MathWorks, Inc.