Board index » delphi » string --> integer

string --> integer

Sorry, I mixed some things up.

my compiler says "Error 26: Type mismatch" for the following line:

  changednumber[i] := 'moddivision';

you need to know:
It's a program to change numbers of base 10 to numbers of base 2..36.

 PROGRAM...
 TYPE NUMBASE = 2..36;

 VAR base :NUMBASE;
     number  :LONGINT;
     changednumber :STRING[36];
     i :INTEGER;

 FUNCTION NumToString(x:LONGINT; b:NUMBASE):STRING;

 VAR newdivision :REAL;
     moddivision,divisionnumber:INTEGER;

BEGIN
    i := 0;
    divisionnumber := x;
    newdivision := 0;

   REPEAT
     i := i + 1;
     newdivision        := divisionnumber / b;
     moddivision    := divisionnumber mod b;
     changednumber[i] := 'moddivision';
     newdivision       := newdivision - (moddivision/b);
     divisionnumber        := newdivision;
   UNTIL (divisionnumber = 0);
 END;

 Thanks in advance.

-Daniel

--
Daniel Knapp <dkn...@gmx.net>
Uni Rostock <daniel.kn...@informatik.uni-rostock.de>

 

Re:string --> integer


Quote
In article <39fc0...@news.uni-rostock.de>, Serge Knapp <dkn...@gmx.net> wrote:
>Sorry, I mixed some things up.

>my compiler says "Error 26: Type mismatch" for the following line:

>  changednumber[i] := 'moddivision';

What on earth are you attempting? The ChangedBumber[i] is a character.
'moddivision' is a string CONSTANT. I assume you instead wanted to
asking the character representing the value in moddivision. The
function to convert a number into character is chr(), but you need to
add 48 (the ASCII code for zero). That is

changednumber[i]:=chr(moddivision+48);

You could also use chr(moddivision+ord('0'))

This however, works only up to base 10. For higher bases you need to
define representation for numbers 10.. As the highest base is 36 I think
it is assumed that letters A..Z. You could either use a string constant
that has all the values, or use a condition:

If moddivision>=10 then changednumber[i]:=chr(.... +ord('A'))
                   else changednumber[i]:=chr(moddivision+ord('0'));

Since this is a home work I have left it partially blank.

Quote

>you need to know:
>It's a program to change numbers of base 10 to numbers of base 2..36.

> PROGRAM...
> TYPE NUMBASE = 2..36;

> VAR base :NUMBASE;
>     number  :LONGINT;
>     changednumber :STRING[36];

Why 36? Was it documented somewhere. Note this has nothing to do with
the maximum base of 36.

Quote
>     i :INTEGER;

> FUNCTION NumToString(x:LONGINT; b:NUMBASE):STRING;

> VAR newdivision :REAL;
>     moddivision,divisionnumber:INTEGER;

>BEGIN
>    i := 0;
>    divisionnumber := x;
>    newdivision := 0;

>   REPEAT
>     i := i + 1;
>     newdivision        := divisionnumber / b;
>     moddivision    := divisionnumber mod b;
>     changednumber[i] := 'moddivision';
>     newdivision       := newdivision - (moddivision/b);
>     divisionnumber        := newdivision;
>   UNTIL (divisionnumber = 0);
> END;

This would produce the number as a mirror image.

Osmo

Re:string --> integer


On Sun, 29 Oct 2000 12:28:02 +0100, "Serge Knapp" <dkn...@gmx.net>
wrote:

Quote
>Sorry, I mixed some things up.

>my compiler says "Error 26: Type mismatch" for the following line:

>  changednumber[i] := 'moddivision';

>you need to know:
>It's a program to change numbers of base 10 to numbers of base 2..36.

...................... snip .....................

 PROGRAM ConvertDecimalToBase;
 CONST symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 TYPE NUMBASE = 2..36;

 VAR base :NUMBASE;
     number  :LONGINT;
     changednumber :STRING[36];    {too long but makes no difference}
     i :INTEGER;

 FUNCTION NumToString(x:LONGINT; b:NUMBASE):STRING;
{ VAR newdivision :LongInt;    
     moddivision,divisionnumber:INTEGER; }
VAR digValue:Integer;
    digChar:Char;
    tmp, syms:String;
BEGIN
   tmp := '';  syms := symbols;
   WHILE x > 0 DO
   BEGIN
        digValue := x MOD b;           {least significant digit value}
        digChar := syms[digValue + 1]; {syms starts at '0'}
        tmp := digChar + tmp;          {build string in reverse}
        x := x div b;
   END;
   NumToString := tmp;
End;

Re:string --> integer


JRS:  In article <CAD264C4158B19BB.2499FA0E94D44227.03E66F037799C839@lp.
airnews.net> of Sun, 29 Oct 2000 12:54:25 seen in
news:comp.lang.pascal.borland, Clif Penn <clifp...@airmail.net> wrote:

Quote
> PROGRAM ConvertDecimalToBase;
> CONST symbols = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
> ...
>        digChar := syms[digValue + 1]; {syms starts at '0'}

IIRC, you don't need that addition if you use
  CONST symbols : array [0..35] of char = '0123... '

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Other Threads