INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How To

How do I implement my own mechanism to encode and decode data to my own standards? by djjd47130
Posted: 25 Feb 13

A small project of mine has lead me to point out how flexible this solution is. I originally needed to make a mechanism to encode/decode strings by using my own replacement values. This turned into a different solution where you can easily specify the replacement values to use. A major advantage to this solution is that it performs only one loop through the string, as opposed to other methods such as StringReplace, which would perform many loops. This allows you to easily register replacement values in one place, rather than code them 2 times for encoding and decoding.

CODE

unit StringEncoding;

interface

type
  TEncodingRule = record
    Original: String;
    Replace: String;
  end;

  TEncodingRules = array of TEncodingRule;

function Encode(const S: String): String;
function Decode(const S: String): String;

implementation

var
  _EncodingRules: TEncodingRules;

function Encode(const S: String): String;
var
  X, Y: Integer;
  R: TEncodingRule;
  T: String;
begin
  Result:= S;
  for X := Length(Result) downto 1 do begin
    for Y := 0 to Length(_EncodingRules) - 1 do begin
      R:= _EncodingRules[Y];
      T:= Copy(Result, X, Length(R.Original));
      if T = R.Original then begin
        Delete(Result, X, Length(R.Original));
        Insert(R.Replace, Result, X);
      end;
    end;
  end;
end;

function Decode(const S: String): String;
var
  X, Y: Integer;
  R: TEncodingRule;
  T: String;
begin
  Result:= S;
  for X := Length(Result) downto 1 do begin
    for Y := Length(_EncodingRules) - 1 downto 0 do begin
      R:= _EncodingRules[Y];
      T:= Copy(Result, X, Length(R.Replace));
      if T = R.Replace then begin
        Delete(Result, X, Length(R.Replace));
        Insert(R.Original, Result, X);
      end;
    end;
  end;
end;

procedure InitEncodingRules;
  procedure A(const Orig, Repl: String);
  begin
    SetLength(_EncodingRules, Length(_EncodingRules) + 1);
    _EncodingRules[Length(_EncodingRules)-1].Original:= Orig;
    _EncodingRules[Length(_EncodingRules)-1].Replace:= Repl;
  end;
begin
  A('_', '_UND_');
  A('%', '_PER_');
  A('&', '_AMP_');
  A(' ', '_SPC_');
  A('=', '_EQU_');
  A('/', '_FSL_');
  A('\', '_BSL_');
  A('(', '_OPR_');
  A(')', '_CPR_');
end;

procedure UninitEncodingRules;
begin
  SetLength(_EncodingRules, 0);
end;

initialization
  InitEncodingRules;
finalization
  UninitEncodingRules;
end. 

As you can see, I'm making use of the initialization and finalization sections of this unit. During the initialization procedure InitEncodingRules, I am adding the rules. This can be changed by simply implementing your own replacements. The few samples above are just so you can understand the general concept.

Once you have customized the replacement values (Rules), you can use Encode and Decode easily to use these rules.

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close