A simple way to encode a text message is to use the ASCII code of its characters and convert them to octal, hexadecimal or binary format.
This method of encoding a text message is simple, straight forward, but in the same time it’s quite easy to decode.
In this tutorial we are going to implement two Scilab custom functions for encoding and decoding text messages. The proposed method is described in the block diagram below.
Our text message to be encoded will be defined in the text file fileTextWord.txt
. The function encodeMessage()
will read the content of the text file and encode the character in either octal, hexadecimal or binary format. The encoded messages is going to be saved in the text file fileTextEncoded.txt
.
To decode the message we’ll use the function decodeMessage()
, which is reading the encoded message text file and converts back to Latin characters text. The result of the decoding is stored in the file fileTextDecoded.txt
. The message should be the same with the initial one from the file fileTextWord.txt
.
Message Encoding
The encoding function is defined in the file encodeMessage.sci
with the following Scilab instructions:
function y=encodeMessage(fileNameIn,fileNameOut,encodingType) clc fi=mopen(fileNameIn,'r'); inputString=mgetl(fi); mclose(fi); decimalMessage = ascii(inputString); switch encodingType case 'o' y=dec2oct(decimalMessage); case 'h' y=dec2hex(decimalMessage); case 'b' y=dec2bin(decimalMessage); end fo = mopen(fileNameOut,'wt'); for i=1:max(size(y)) mfprintf(fo,'%s ',y(i)); end mclose(fo); endfunction
The encoding function has three arguments:
fileNameIn
– string, representing the name and extension of the file where the text to be encoded is defined; for our example is going to be 'fileTextWord.txt'
fileNameOut
– string, representing the name and extension of the file where the encoded message will be saved; for our example is going to be 'fileTextEncoded.txt'
encodingType
– string, representing the type of encoding of the message; there are only three options: 'o'
for octal, 'h'
for hexadecimal and 'b'
for binary
How does it work?
First we open the text file with the Scilab function mopen()
. The content of the file is read with the function mgetl()
and stored in the local string variable inputString
. After we read the entire file, we close it with the function mclose()
. Further, the Latin characters are converted in ASCII code with the function ascii()
and the values stored in the vector decimalMessage
.
Next, depending on the type of encoding, we convert the ASCII codes in either octal, hexadecimal or binary number representation, using a select-case conditional statement and the embedded Scilab functions dec2oct()
, dec2hex()
or dec2bin()
.
After the conversion is done, the characters are written in another text file with function mfprintf()
, called within a for-loop.
Suppose we want to encode the following message:
Let’s meet at your place and plan how to take over the world!
First, we open a text editor, write the message and save it as a *.txt
file.
Second, call the function encodeMessage()
and encode the message in octal representation:
encodeMessage('fileTextWord.txt','fileTextEncoded.txt','o')
If we open the encoded text file fileTextEncoded.txt
, we should see the following encoding:

Image: Octal encoding of the text file
Close the file.
Encoding in hexadecimal format is done with the following function call:
encodeMessage('fileTextWord.txt','fileTextEncoded.txt','h')
Opening the same file should display the following text encoding:
Close the file.
If binary format is out preferred option, we can apply it with the function call:
encodeMessage('fileTextWord.txt','fileTextEncoded.txt','b')
Opening the same file should display the following text encoding:
Message Decoding
Now that we have our encoded message, we should be able to decode it. The decoding function is defined in the file decodeMessage.sci
with the following Scilab instructions:
function y=decodeMessage(fileNameIn,fileNameOut,decodingType) clc fi=mopen(fileNameIn,'r'); inputString=mgetl(fi); mclose(fi); inputString=tokens(inputString); select decodingType case 'o' y=strcat(char(oct2dec(inputString))); case 'h' y=strcat(char(hex2dec(inputString))); case 'b' y=strcat(char(bin2dec(inputString))); end fo = mopen(fileNameOut,'wt'); for i=1:max(size(y)) mfprintf(fo,'%s ',y(i)); end mclose(fo); endfunction
The decoding function has three arguments:
fileNameIn
– string, representing the name and extension of the file with the encoded text message; for our example is going to be 'fileTextEncoded.txt'
fileNameOut
– string, representing the name and extension of the file where the decoded message will be saved; for our example is going to be 'fileTextDecoded.txt'
decodingType
– string, representing the type of encoding of the message; there are only three options: 'o'
for octal, 'h'
for hexadecimal and 'b'
for binary
The decoding algorithm is very similar with the encoding one, the major difference being the reverse functions for the character conversion: oct2dec()
, hex2dec()
and bin2dec()
.
For a better understanding on how the Scilab functions tokens()
, char()
and strcat()
work, read the article Mastering strings in Scilab.
Let’s suppose that our encoding was in binary format. To decode the message we need to call the following function:
decodeMessage('fileTextEncoded.txt','fileTextDecoded.txt','b')
Opening the fileTextDecoded.txt
should revel the same text message as in the initial file.
If we want to decode a another (third party) message we only have to copy and past it in the file fileTextEncoded.txt
and run the decoding function. Of course, we should know the encoding of the message or guess it by looking at the characters.
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!