Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-08

How can I increment/decrement a alphanumeric value?

24 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

dmitry boyarintsev
to silvioprog
silvioprog wrote 14 lines
?E.g: 001 002 .. 00A 00B .. 0A0 0A1 .. ZZZ Is there a native routine in Free Pascal to do that?

Feels like a college task.
There's no such routine.
You can write a routine that converts alphanumeric number to integer and back. Since it's easier to evaluate integers.

thanks,
dmitry

Reply by email

bobby
to dmitry boyarintsev
dmitry boyarintsev wrote 29 lines
On Thu, Aug 19, 2010 at 7:36 PM, silvioprog wrote: > E.g: > > 001 > 002 > .. > 00A > 00B > .. > 0A0 > 0A1 > .. > ZZZ > > Is there a native routine in Free Pascal to do that? Feels like a college task. There's no such routine. You can write a routine that converts alphanumeric number to integer and back. Since it's easier to evaluate integers. thanks, dmitry -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Hi,

I have a function for increasing HEX values, based on string manipulations, not on conversion to integer and back. Would that be of any help?

bobby

Reply by email

silvioprog
to dmitry boyarintsev: Feels like a college task. There's no such routine. You can write a routine that converts…
dmitry boyarintsev wrote 8 lines
there a native routine in Free Pascal to do that? Feels like a college task. There's no such routine. You can write a routine that converts alphanumeric number to integer and back. Since it's easier to evaluate integers. thanks, dmitry

Hi dmitry,

I need to increment (and if possible, decrement) a VarChar field.

Thanks for the help, it's an interesting idea to convert to integer and later increase. :)

Silvio Cl?cio

Reply by email

silvioprog
to bobby: I have a function for increasing HEX values, based on string manipulations, not on…
bobby wrote 7 lines
Hi, I have a function for increasing HEX values, based on string manipulations, not on conversion to integer and back. Would that be of any help? bobby

Hi bobby,

Would you show me the function?

Here's one I received from a friend here in Brazil:

[code]
{$J+}
const
   CIncStrChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
{$J-}
function IncStr(const AString: string): string;
var
   VCurrChar: PChar;
   I, VPos, VBackIndex: Integer;
begin
   Result := Copy(AString, 1, Length(AString));
   for I := 1 to Length(AString) do
     if Pos(AString[I], CIncStrChars) < 1 then
       raise Exception.Create('String contains an invalid character');
   if Length(AString) < 1 then
   begin
     Result := CIncStrChars[1];
     Exit;
   end;
   if Length(AString) < 2 then
   begin
     Result := Succ(AString[1]);
     Exit;
   end;
   VBackIndex := 0;
   while True do
   begin
     VCurrChar := @Result[Length(Result) - VBackIndex];
     VPos := Pos(VCurrChar^, CIncStrChars);
     if VPos = Length(CIncStrChars) then
     begin
       VCurrChar^ := CIncStrChars[1];
       if (Length(Result) - VBackIndex) <= 1 then
       begin
         Result := CIncStrChars[2] + Result;
         Break;
       end
       else
       begin
         Inc(VBackIndex);
         Continue;
       end;
     end
     else
     begin
       VCurrChar^ := CIncStrChars[VPos + 1];
       Break;
     end;
   end;
end;

Author: http://code.google.com/u/kingbizugo/
[/code]

I'm seeing with him to creating the DecStr function.

Thanks bobby. :) (and sorry for my english :p ).

Silvio Cl?cio

Reply by email

waldo kitty
to dmitry boyarintsev: Feels like a college task. There's no such routine. You can write a routine that converts…
dmitry boyarintsev wrote 17 lines
On Thu, Aug 19, 2010 at 7:36 PM, silvioprog wrote: > E.g: > > 001 > 002 > .. > 00A > 00B > .. > 0A0 > 0A1 > .. > ZZZ > > Is there a native routine in Free Pascal to do that? Feels like a college task.

or maybe even freshman high school...

There's no such routine. You can write a routine that converts alphanumeric number to integer and back. Since it's easier to evaluate integers.

can't

var foo char;

inc(foo)?

there's also always the array[0..Z] method, too ;)

breaking out into individual positions in the "string" is something else to play with :P

Reply by email

bobby
to silvioprog: Would you show me the function? Here's one I received from a friend here in Brazil: [code]
silvioprog wrote 13 lines
Em 19/08/2010 17:26, bobby escreveu: > Hi, > > I have a function for increasing HEX values, based on string > manipulations, not on conversion to integer and back. > Would that be of any help? > > bobby Hi bobby, Would you show me the function?

Hi,

here it is:

function incHexStr(inHexStr: string): string;
var
   HexMap: string;
   i: integer;
   j: integer;
   s: string;
   carry: integer;
begin
   result := '';
   s := '';
   carry := 0;
   HexMap := '0123456789abcdef0123456789abcdef';
   for j := 1 to 16 do
     if AnsiLowerCase(inHexStr[length(inHexStr)]) = HexMap[j] then
     begin
       if j > 15 then carry := 1;
       inHexStr[length(inHexStr)] := HexMap[(j + 1)];
       break;
     end;
   if carry = 1 then
     for i := (length(inHexStr) - 1) downto 1 do
     begin
       if carry = 1 then
         for j := 1 to 16 do
           if AnsiLowerCase(inHexStr[i]) = HexMap[j] then
           begin
             carry := 0;
             if j > 15 then carry := 1;
             inHexStr[i] := HexMap[(j + 1)];
             break;
           end;
     end;
   if carry = 1 then inHexStr := '01' + inHexStr;
   result := inHexStr;
end;

Reply by email

silvioprog
to bobby: here it is: function incHexStr(inHexStr: string): string; var HexMap: string; i: integer;…
bobby wrote 44 lines
On 8/20/2010 5:01 AM, silvioprog wrote: > Hi bobby, > > Would you show me the function? Hi, here it is: function incHexStr(inHexStr: string): string; var HexMap: string; i: integer; j: integer; s: string; carry: integer; begin result := ''; s := ''; carry := 0; HexMap := '0123456789abcdef0123456789abcdef'; for j := 1 to 16 do if AnsiLowerCase(inHexStr[length(inHexStr)]) = HexMap[j] then begin if j > 15 then carry := 1; inHexStr[length(inHexStr)] := HexMap[(j + 1)]; break; end; if carry = 1 then for i := (length(inHexStr) - 1) downto 1 do begin if carry = 1 then for j := 1 to 16 do if AnsiLowerCase(inHexStr[i]) = HexMap[j] then begin carry := 0; if j > 15 then carry := 1; inHexStr[i] := HexMap[(j + 1)]; break; end; end; if carry = 1 then inHexStr := '01' + inHexStr; result := inHexStr; end;

Thank you very much, bobby. ;)

Silvio Cl?cio

Reply by email

Antônio
to Henry Vermaak

S?lvio,

Your routine, after some modifications, seems to do the job:

{$J+}
const
 CIncStrChars = '0123456789ABCDEF';
{$J-}
function IncStr(const AString: string): string;
var
 VCurrChar: PChar;
 I, VPos, VBackIndex: Integer;
begin
 Result := Copy(AString, 1, Length(AString));
 for I := 1 to Length(AString) do
   if Pos(AString[I], CIncStrChars) < 1 then
     raise Exception.Create('String contains an invalid character');
 if Length(AString) < 1 then
 begin
   Result := CIncStrChars[1];
   Exit;
 end;
 {if Length(AString) < 2 then
 begin
   Result := Succ(AString[1]);
   Exit;
 end;}
 VBackIndex := 0;
 while True do
 begin
   VCurrChar := @Result[Length(Result) - VBackIndex];
   VPos := Pos(VCurrChar^, CIncStrChars);
   if VPos = Length(CIncStrChars) then
   begin
     VCurrChar^ := CIncStrChars[1];
     if (Length(Result) - VBackIndex) <= 1 then
     begin
       Result := CIncStrChars[2] + Result;
       Break;
     end
     else
     begin
       Inc(VBackIndex);
       Continue;
     end;
   end
   else
   begin
     VCurrChar^ := CIncStrChars[VPos + 1];
     Break;
   end;
 end;
end;

Reply by email

silvioprog
to Alberto Narduzzi
Alberto Narduzzi wrote 20 lines
Ant?nio, why reinvent the wheel...? as already pointed out by Henry yesterday: http://freepascal.org/docs-html/rtl/strutils/dec2numb.html http://freepascal.org/docs-html/rtl/strutils/numb2dec.html it is as easy as: Uses strutils; Procedure IncrementHex(Var HexS:String); Begin HexS := dec2numb(numb2dec(HexS) + 1); End; Cheers, A.

Hi,

This does not solve my problem, I need to use characters from A to Z.

Thanks,

Silvio Cl?cio

Reply by email

Antônio
to Alberto Narduzzi: why reinvent the wheel...? as already pointed out by Henry yesterday:…

I had not seen Henry's routine.

Ant?nio

2010/8/21 Alberto Narduzzi :

Show quoted text 27 lines
Ant?nio, why reinvent the wheel...? as already pointed out by Henry yesterday: http://freepascal.org/docs-html/rtl/strutils/dec2numb.html http://freepascal.org/docs-html/rtl/strutils/numb2dec.html it is as easy as: Uses strutils; Procedure IncrementHex(Var HexS:String); Begin ?HexS := dec2numb(numb2dec(HexS) + 1); End; Cheers, A. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

silvioprog
to Wildfire
Wildfire wrote 5 lines
> http://freepascal.org/docs-html/rtl/strutils/dec2numb.html > http://freepascal.org/docs-html/rtl/strutils/numb2dec.html Silvio, read the links, those two functions provide exactly what you want (use BASE 36)

Wow, perfectly correct,

procedure IncrementHex(var HexS: string);
begin
   HexS := dec2numb(numb2dec(HexS, 36) + 1, 3, 36);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
   S: string;
begin
   S := Edit1.Text;
   IncrementHex(S);
   Edit2.Text := S;
end;

Yes, thanks mens :)

Silvio Cl?cio

Reply by email

Antônio
to silvioprog

Hexadecimals are base 16, with numbers from 0 to 9 and letters from A to F.

Ant?nio

2010/8/21 silvioprog :

Show quoted text 23 lines
?uses ?StrUtils; function IncStr(const AString: string; const ADigits: Byte = 3): string; begin ?Result := Dec2Numb(Numb2Dec(AString, 36) + 1, ADigits, 36); end; procedure TForm1.Button1Click(Sender: TObject); begin ?Edit2.Text := IncStr(Edit1.Text); end; Thats all ;) Silvio Cl?cio -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Wildfire
to Antônio

----- Original Message -----

Earlier message 15 lines
From: "Ant?nio" To: "Lazarus mailing list" Sent: Saturday, August 21, 2010 3:09 PM Subject: Re: [Lazarus] How can I increment/decrement a alphanumeric value? Hexadecimals are base 16, with numbers from 0 to 9 and letters from A to F. Ant?nio 2010/8/21 silvioprog : Antonio, Silvio requires base 36 (0-9,A-Z) not base 16, I think this is where the confusion has come from.

Reply by email

Antônio
to Antônio

There is a problem wiht Num2Dec.

for
Hex := 'G'
Dec2Numb(Numb2Dec(Hex, 16) - 1, {Length(Hex)} 2, 16) will not refuse 'G' as an invalid hexadecimal number. The result would be 'F'.

Ant?nio

Reply by email