Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Transform integer into a coded string

Posted on 2004-08-08
5
223 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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