Link to home
Start Free TrialLog in
Avatar of rincewind666
rincewind666

asked on

Changing numbers/non-numbers into numbers only

I need to change a sequence of numbers and non-numbers (groups of possibly 4 but not always) which are separated by dashes into another related sequence of only numbers (always length of 45 in groups of 5) but also separated by dashes.

EXAMPLE:

9244-A4AE-43F4-6598
into
92442-42243-24659-85948-20880-55838-24514-765879-23412

One way of doing this by using the first sequence:

Change non-numbers into 2's (9244-2422-4324-6598)
Remove dashes (9244242243246598)
Add random number/s at end to make 45 characters (9244242243246598 + 594820880558382451476587923412)
Insert a dash after every 5 characters (92442-42243-24659-85948-20880-55838-24514-765879-23412)

You may know a better way.  I need the code to do this.  I am using Delphi 6.  Your help would be greatly appreciated.  Thanks.
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

So you want to change letter by number 2?
9244-A4AE-43F4-6598
9244-2422-4324-6598
Avatar of rincewind666
rincewind666

ASKER

Yes or any other number
try this function

function GetMyCode(str: string):string;
const number: string = '1234567890';
var I: integer;
    rand: word;
begin
str := StringReplace(str, '-', '', []);
for I := 1 to Length(str) do
if Pos(str[I], Number) = 0 then
   str := StringReplace(str, str[I], '2', []);
for I := 1 to 11 do
str := str + IntToStr(random(999));
for I := 1 to Length(str) do
if I mod 5 = 0 then
if result = '' then result := copy(str, 1, 5)
else result := result + '-' +copy(str, I - 5, 5) ;
end;
ASKER CERTIFIED SOLUTION
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry but it doesn't quite work, although I think it's nearly there.  I get a number like
92442-24222-43242-65980-31860-20227-26703-18161-37142
instead of something like
92442-42243-24659-85948-20880-55838-24514-765879-23412
Note the first groups.

Also, sometimes I get something like
92442-24222-43242-65987-17306-16232-94652-46824-27848-11498
which is 10 groups instead of 9.

I appreciate your help with this.
You found this result with last post (update)?
Opps!  Missed that.  I will try again.
Hi, this does exactly what you want:

procedure TForm1.Button1Click(Sender: TObject);
begin
 ShowMessage(FixString('9244-A4AE-43F4-6598'));
end;

function TForm1.FixString(s: String): String;
var
 i: Integer;
begin
 Result := StringReplace(s, '-', '', [rfReplaceAll]);
 for i := 1 to Length(Result) do
  if Result[i] > '9' then
   Result[i] := '2';
 while Length(Result) < 45 do
  Result := Result + IntToStr(Random(9999));
 Result := Copy(Result, 1, 45);
 for i := 1 to Length(Result) + 5 do
  if i mod 6 = 0 then
   Insert('-', Result, i);
end;
Excellent!  It works perfectly.  Many thanks for your help Mahdi78.  It is geatly appreciated.
Excellent!  It works perfectly.  Many thanks for your help Mahdi78.  It is greatly appreciated.
You are welcome, i am glad to help you ;)
Remove the following line
 Form1.Edit1.Text := str;
Yes.  I saw that.  Thanks.