Solved

Transform integer into a coded string

Posted on 2004-08-08
5
224 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 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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