Solved

Transform integer into a coded string

Posted on 2004-08-08
5
220 Views
Last Modified: 2010-04-05
I would like to do the following:

I have a integer which I would like to transform into a certain string (so it is not recognizable as a number).

Example:
Integer = 250

The digits must be translated into capital alpha's.
0 := K, 1 := B, 2 := M, 3 := P, 4 := F, 5 := W, 6 := R, 7 := S, 8 := O, 9 := Z

I would like to do this in a function with a less as code as possible.
Who can supply me with such a function.

This is what I have now:

function GetalCoderen(Const N : Integer) : String;
var GecodeerdGetal : String;
    L, P : Integer;
begin
  GecodeerdGetal := IntToStr(N);
  L     := Length(GecodeerdGetal); //Hulpvariabele voor lengte
  P     := 1;             //Positie binnen de tekst
  While P <= L do
  begin
    // Translate digits into code > 0 := K, 1 := B, 2 := M, 3 := P, 4 := F, 5 := W, 6 := R, 7 := S, 8 := O, 9 := Z
    If (GecodeerdGetal[P] = '0') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('K',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '1') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('B',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '2') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('M',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '3') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('P',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '4') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('F',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '5') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('W',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '6') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('R',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '7') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('S',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '8') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('O',GecodeerdGetal,P);
    end;
    If (GecodeerdGetal[P] = '9') then
    begin
      Delete(GecodeerdGetal,P,1);
      Insert('Z',GecodeerdGetal,P);
    end;
    Inc(P);
  end;
  Result := GecodeerdGetal;
end;
0
Comment
Question by:Delphiwizard
5 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11750506
function TransformInteger(Value : Integer) : String;
type
  TransformArray = array[0..9] of Char;
var
  Arr            : TransformArray;
  tmp_str        : String;
  tmp_Result     : String;
  counter        : Integer;
begin
  // init the array
  Arr[0] := 'K';     Arr[1] := 'B';     Arr[2] := 'M';
  Arr[3] := 'P';     Arr[4] := 'F';     Arr[5] := 'W';
  Arr[6] := 'R';     Arr[7] := 'S';     Arr[8] := 'O';
  Arr[9] := 'Z';
  // get the integer as String
  tmp_str := IntToStr(Value);
  // init the result
  tmp_Result := '';
  for counter := length(tmp_str) downto 1 do
    tmp_Result := tmp_Result + Arr[counter-1];
  // return the result
  Result := tmp_Result;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11750517
function  GetalCoderen(const N: Integer): string;
var
  I, L, P:             Integer;
  C:                   Char;
  SArr:                string;
  SRes:                string;
  GecodeerdGetal:      string;
begin
  SRes := '';
  SArr := 'KBMPFWRSOZ';
  GecodeerdGetal := IntToStr(N);
  L     := Length(GecodeerdGetal); //Hulpvariabele voor lengte
  for P := 1 to L do
  begin
    I := StrToInt(GecodeerdGetal[P]);
    C := SArr[I+1];
    SRes := SRes + C;
  end;
  Result := Sres;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  Memo.Lines.Add(GetalCoderen('1234567890')));
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 11750526
function GetalCoderen(Const N : Integer) : String;
var
  Letters :  Variant;
  i: integer;
begin
  Letters := VarArrayCreate([1, 10], varVariant);
  Letters := VarArrayOf(['K', 'B', 'M', 'P', 'F', 'W', 'R', 'S', 'O', 'Z']);
  try
    result := IntToStr(N);
    for i := 0 to 9 do begin
      result := StringReplace(result, IntToStr(i), String(Letters[i]), [rfReplaceAll]);
    end;
  except
    result := ''; // or raise an exception
  end;
end;

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 250 total points
ID: 11750543
or simplier (thanks to esoftbg :-)

function GetalCoderen(Const N : Integer) : String;
var
  Letters :  string;
  i: integer;
begin
  Letters := 'KBMPFWRSOZ';
  try
    result := IntToStr(N);
    for i := 0 to 9 do begin
      result := StringReplace(result, IntToStr(i), String(Letters[i+1]), [rfReplaceAll]);
    end;
  except
    result := ''; // or raise an exception
  end;
end;

Regards, Geo
0
 

Author Comment

by:Delphiwizard
ID: 11750583
Perfect Geo, Thanks to all
But the smallest got the points.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now