En- And Decoding Simple Strings

BlitzMax Forums/BlitzMax Tutorials/En- And Decoding Simple Strings

Sensenwerk(Posted 2005) [#1]
This tutorial explains a very basic way to encode strings and decode them again to get the original string.


Strict

Local str:String = "Oh no, I'm a lonely string...";
Print "source string: " + str;

Local ENstr:String = Encode(str);
Print "encoded string: " + ENstr;
Local DEstr:String = Decode(ENstr);
Print "decoded string: " + DEstr;



Function Encode:String(Text:String)  ' using shift left

	Local EncText:String = "";
	
	For Local i:Int = 0 To (Len(Text)-1)
		EncText:+ Chr((Text[i] Shl 1)*5);
	Next
	
	Return EncText;
	
EndFunction



Function Decode:String(Text:String)  ' using shift right

	Local DecText:String = "";
	
	For Local i:Int = 0 To (Len(Text)-1)
		DecText:+ Chr((Text[i] Shr 1)/5);
	Next
	
	Return DecText;
	
EndFunction


This can be used in combination with file writing to create encrypted highscores, savegames, userlogs etc.



Let's see, what this code does:
First we use strict to ensure clean code. Then we creat a string 'str' containing some text and display this text.

Strict

Local str:String = "Oh no, I'm a lonely string...";
Print "source string: " + str;



Now we creat an other string 'ENstr' which will contain our 'str' string, but encoded.

Local ENstr:String = Encode(str);



We take a look at our Encode() function (I added line numbers to allow easier exlaining)...

01 Function Encode:String(Text:String) ' using shift left
02
03 Local EncText:String = "";
04
05 For Local i:Int = 0 To (Len(Text)-1)
06 EncText:+ Chr((Text[i] Shl 1)*5);
07 Next
08
09 Return EncText;
10
11 EndFunction


Line one tells us we want a function of return type string accepting a string as parameter.
Line three defines a local string which will contain our encoded string which will be returned at the end of the function.
Line five starts a loop which will be repeated until we have reached the end of our text string.
Line six adds every time the loop is repeated the value of the current char at position (i+1) but shifted Left by 1 'Shl 1' and then multiplied by 5, but finally converted into a char using chr() to allow us to add it to our string.
This line is the actually encoding line.
You can customize the shift value or multiplier if you want.
But always remember to change your decode function in the same way, or your decoded string will be unreadable.

A short look at the important line of our decode function

DecText:+ Chr((Text[i] Shr 1)/5);


shows us, that this is the same as the encodeing but inverted: using 'shr 1' (shift right 1) and dividing it by 5 we transform our encoded string back into our original 'str'.

The last little bit of our code:

Print "encoded string: " + ENstr;


We output our endcoded string (it should look scary :),
then we creat a last string which will contain the reencoded (decoded) string and display it:

Local DEstr:String = Decode(ENstr);
Print "decoded string: " + DEstr;


Hopefully you see the same text as our source string.


PS: Sorry for my sometimes a bit poor english... I'm not from USA or UK... :|

Additional notes:
.Of course you don't need to multiply the shifted char, but this makes it a lot more difficult to decode things, if you don't know the shift and multiplier value.
.Also note that using shifts of higher values can cause the char to be out of range, i.e. empty: Your string will be lost.
Using higher multiplieres should be possible without data loss.