E.g:
001
002
..
00A
00B
..
0A0
0A1
..
ZZZ
Is there a native routine in Free Pascal to do that?
Tahnks,
Silvio Cl?cio

Mailing lists / lazarus / 2010-08
24 messages · 9 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
E.g:
001
002
..
00A
00B
..
0A0
0A1
..
ZZZ
Is there a native routine in Free Pascal to do that?
Tahnks,
Silvio Cl?cio
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,
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
E.g: 001 002 .. 00A 00B .. 0A0 0A1 .. ZZZ Is there a native routine in Free Pascal to do…
write a convert procedure from base-10 numbers into base-36 and you're done.
Feels like a college task. There's no such routine. You can write a routine that converts…
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
I have a function for increasing HEX values, based on string manipulations, not on…
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
write a convert procedure from base-10 numbers into base-36 and you're done.
Alberto Narduzzi wrotewrite a convert procedure from base-10 numbers into base-36 and you're done.
I'll try this. To create DecStr function.
Thanks Alberto :)
Silvio Cl?cio
Feels like a college task. There's no such routine. You can write a routine that converts…
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
Would you show me the function? Here's one I received from a friend here in Brazil: [code]
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;write a convert procedure from base-10 numbers into base-36 and you're done.
write a convert procedure from base-10 numbers into base-36 and you're done.
IMO such conversion procedures, with selectable base, should become part of the RTL.
DoDi
here it is: function incHexStr(inHexStr: string): string; var HexMap: string; i: integer;…
Thank you very much, bobby. ;)
Silvio Cl?cio
write a convert procedure from base-10 numbers into base-36 and you're done.
http://freepascal.org/docs-html/rtl/strutils/dec2numb.html http://freepascal.org/docs-html/rtl/strutils/numb2dec.html
Henry
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;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
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 :
This does not solve my problem, I need to use characters from A to Z. Silvio Cl?cio
This does not solve my problem, I need to use characters from A to Z.
Thanks,
Silvio Cl?cio
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
This does not solve my problem, I need to use characters from A to Z. Silvio Cl?cio…
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
Hexadecimals are base 16, with numbers from 0 to 9 and letters from A to F.
Ant?nio
2010/8/21 silvioprog :
----- Original Message -----
why reinvent the wheel...? as already pointed out by Henry yesterday:…
why reinvent the wheel...?
Square wheels are safer.
http://en.wikipedia.org/wiki/File:Rolling-Square.gif
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
Numb2Dec performs no validation of the Number provided, so if you provide something like '/', the function will return a Dec.
Ant?nio