Help!
Author Message
Help!

I know this is probably a very basic thing, but I'm new to modula-2

I need to convert a character variable to a cardinal, and am stuck.
I keep getting the values position in the ASCII table instead of the
numeric value. ( I think anyway )

b.

Thu, 04 Oct 2001 03:00:00 GMT
Help!

Quote:

> I know this is probably a very basic thing, but I'm new to modula-2

> I need to convert a character variable to a cardinal, and am stuck.
> I keep getting the values position in the ASCII table instead of the
> numeric value. ( I think anyway )

I think this is a fair question. What you're doing is ORD(c), and finding
that ORD('1') = 31H rather than 1.

Try ORD(c) - ORD('0')

and we'll be very impressed if you come back and tell us (i) why it works,
(ii) why I've put ORD('0') rather than a simple number, and (iii) what the
"gotchas" are :-)

-- Mark Morgan Lloyd

--
-- [Opinions above are the author's, not those of his employers or
colleagues]
--
-- ** Internet feed may be unreliable- please cc anything important by
email **

Thu, 04 Oct 2001 03:00:00 GMT
Help!

Quote:

>I know this is probably a very basic thing, but I'm new to modula-2

>I need to convert a character variable to a cardinal, and am stuck.
>I keep getting the values position in the ASCII table instead of the
>numeric value. ( I think anyway )

>b.

The value position in the ASCII table? But that IS the value!

e.g. VAL(CARDINAL,"A") = 65

What else do you want?

Just do something like :

FROM IO IMPORT RdChar, WrCard;
VAR
c : CHAR;
i : CARDINAL;

REPEAT
c := RdChar();
i := VAL(CARDINAL,c);
WrCard(i,4);
UNTIL (c='*');

Now if you want the values to be A=1, B=2 etc, just substract 64 (or

i>=65)
So you will have :

Regards,

Rob

Thu, 04 Oct 2001 03:00:00 GMT
Help!

Quote:

>> I know this is probably a very basic thing, but I'm new to modula-2

>> I need to convert a character variable to a cardinal, and am stuck.
>> I keep getting the values position in the ASCII table instead of the
>> numeric value. ( I think anyway )

>I think this is a fair question. What you're doing is ORD(c), and finding
>that ORD('1') = 31H rather than 1.

>Try ORD(c) - ORD('0')

>and we'll be very impressed if you come back and tell us (i) why it works,
>(ii) why I've put ORD('0') rather than a simple number, and (iii) what the
>"gotchas" are :-)

>-- Mark Morgan Lloyd

>--
>-- [Opinions above are the author's, not those of his employers or
>colleagues]
>--
>-- ** Internet feed may be unreliable- please cc anything important by
>email **

Thanks everyone who gave info,

1) yes it did work, and I have a better understanding of processing
input.
2) i'm not exactly sure how it works, but I know it has something to
do with working out the actual value from the position in the ASCII
table. (just haven't got time at the moment to anaylse it )

thanks again,

beth.

Fri, 05 Oct 2001 03:00:00 GMT
Help!

Quote:

> > I know this is probably a very basic thing, but I'm new to modula-2

> > I need to convert a character variable to a cardinal, and am stuck.
> > I keep getting the values position in the ASCII table instead of the
> > numeric value. ( I think anyway )

> I think this is a fair question. What you're doing is ORD(c), and finding
> that ORD('1') = 31H rather than 1.

Alternatively, if you want to convert strings to numbers in general, I
seem to remember that (in PIM3 at least) one uses VAL.

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
Takeoff is optional.
Landing (sooner or later) is mandatory.

Fri, 05 Oct 2001 03:00:00 GMT
Help!

Quote:

> e.g. VAL(CARDINAL,"A") = 65

> What else do you want?

> Just do something like :

He wants to convert "1":char to 1:cardinal

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
Takeoff is optional.
Landing (sooner or later) is mandatory.

Fri, 05 Oct 2001 03:00:00 GMT
Help!

Quote:

> Alternatively, if you want to convert strings to numbers in general, I
> seem to remember that (in PIM3 at least) one uses VAL.

ORD(x) is a shorthand form of VAL(CARDINAL, x)

Andreas.

--
Andreas Borchert, Universitaet Ulm, SAI, Helmholtzstr. 18, 89069 Ulm,  Germany

WWW:    http://www.mathematik.uni-ulm.de/sai/borchert/

Sat, 06 Oct 2001 03:00:00 GMT
Help!

Quote:

> Alternatively, if you want to convert strings to numbers in general, I
> seem to remember that (in PIM3 at least) one uses VAL.

No. Wouldn't apply the required DEC(). VAL() (certainly as per PIM) is a
type-transfer with some degree of range checking and slightly-improved
semantics when the result and operand sizes differ, I think the original
intention was to make it the inverse of ORD().

-- Mark Morgan Lloyd

--
-- [Opinions above are the author's, not those of his employers or
colleagues]
--
-- ** Internet feed may be unreliable- please cc anything important by
email **

Sat, 06 Oct 2001 03:00:00 GMT
Help!
On Mon, 19 Apr 1999 19:07:24 +0100, Martin Harvey

Quote:

>> e.g. VAL(CARDINAL,"A") = 65
>> What else do you want?
>> Just do something like :
>He wants to convert "1":char to 1:cardinal
>--
>Martin Harvey.
>http://www.harvey27.demon.co.uk/mch24/

MODULE CharToCard;

FROM InOut IMPORT Write, WriteCard, WriteLn;

VAR     ch : CHAR;
a  : CARDINAL;

BEGIN

ch := '1';
a := ORD(ch) - ORD('0');

Write(ch);
WriteLn;
WriteCard(a,1);
WriteLn;

END CharToCard.

Greg_
http://www.korax.net/~telic

Sat, 06 Oct 2001 03:00:00 GMT
Help!

Quote:

> Thanks everyone who gave info,

> 1) yes it did work, and I have a better understanding of processing
> input.
> 2) i'm not exactly sure how it works, but I know it has something to
> do with working out the actual value from the position in the ASCII
> table. (just haven't got time at the moment to anaylse it )

> thanks again,

> beth.

Beth,

The following table will hold you in good stead for very many years if
you don't already have a copy. You should be able to figure out why the
solution works given it.

MH.

00   10   20   30   40   50   60   70
--------------------------------------

1 | SOH  DC1   !    1    A    Q    a    q
2 | STX  DC2   "    2    B    R    b    r
3 | ETX  DC3   #    3    C    S    c    s
4 | EOT  DC4   \$    4    D    T    d    t
5 | ENQ  NAK   %    5    E    U    e    u
6 | ACK  SYN   &    6    F    V    f    v
7 | BEL  ETB   '    7    G    W    g    w
8 |  BS  CAN   )    8    H    X    h    x
9 |  HT   EM   (    9    I    Y    i    y
A |  LF  SUB   *    :    J    Z    j    z
B |  VT  ESC   +    ;    K    [    k    {
C |  FF   FS   ,    <    L    \    l    |
D |  CR   GS   -    ]    M    ]    m    }
E |  SO   RS   .    >    N    ^    n    ~
F |  SI   US   /    ?    O    _    o   DEL

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
Takeoff is optional.
Landing (sooner or later) is mandatory.

Sat, 06 Oct 2001 03:00:00 GMT

 Page 1 of 1 [ 10 post ]

Relevant Pages