?
Solved

Transform integer into a coded string

Posted on 2004-08-08
5
Medium Priority
?
227 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

752 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