Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Transform integer into a coded string

Posted on 2004-08-08
5
Medium Priority
?
228 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
[X]
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
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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 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