Getting a CARDINAL into a zero-padded ARRAY OF CHAR 
Author Message
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Please forgive the bone-headedness of this question: I'm a C programmer having
difficulties making the switch to M2.

I have a value in a cardinal, say 13. I want to get this value into an array
of char formatted like "00013". How do I do this?

Many thanks,
  Jeff.

--



Tue, 05 Aug 1997 14:51:34 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:

>Please forgive the bone-headedness of this question: I'm a C programmer having
>difficulties making the switch to M2.
>I have a value in a cardinal, say 13. I want to get this value into an array
>of char formatted like "00013". How do I do this?
>Many thanks,
>  Jeff.
>--


--------------------------------------
If you are not running an ISO-compliant compiler, then you will have
to check the documentation of the libraries that the compiler makes
available. If, however, you are using an ISO-compliant compiler, you
can use the procedure WholeStr.CardToStr to do the conversion for you;
it is declared as:
  PROCEDURE CardToStr (card: CARDINAL; VAR str: ARRAY OF CHAR);

You have run across the worst problem in Modula-2 (as compared to C):
the fact that until ISO-compliant compilers become generally available,
there is a distinct paucity of standardized libraries.

                                  -- Aron



Wed, 06 Aug 1997 08:21:05 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:

>Please forgive the bone-headedness of this question: I'm a C programmer having
>difficulties making the switch to M2.

>I have a value in a cardinal, say 13. I want to get this value into an array
>of char formatted like "00013". How do I do this?

>Many thanks,
>  Jeff.

>--


        First, you must convert from number to string and then get
        the string length. Then subtract from 5 (your implied string
        length) this string length. Now you know how 0's you need to
        prepend.  Fill a buffer (via a loop and concatenation) with
        the required number of 0's and then concatenate the converted
        string.

        Voila,
                RLR

        PS.  How's the weather up there - Buffalo is coming around.
--
                                          ///'
                                    __   (o|o)   __



Thu, 07 Aug 1997 16:16:29 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:

>I have a value in a cardinal, say 13. I want to get this value into an array
>of char formatted like "00013". How do I do this?

It depends on what libraries you have; but, in my experience, most
libraries don't give you the zero-padding option.

It's easy enough to write your own procedure to do the job.
The following code is typed directly into a rather unfriendly
editor (vi), and is untested, but should be near enough to correct.

PROCEDURE CardToZeroPadded (x, N: CARDINAL;  VAR (*OUT*) result: ARRAY OF CHAR);

    (* Converts x to an N-digit string, with leading zeroes. *)

    BEGIN
        FOR j := N-1 TO 0 BY -1 DO
            result[j] := CHR(ORD('0') + x MOD 10);
            x := x DIV 10;
        END (*FOR*);
    END CardToZeroPadded;

--

                           file://ee.newcastle.edu.au/pub/www/Moylan.html



Fri, 08 Aug 1997 06:41:33 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR
[Snip]
Quote:
> Is it time to give up on Modula-2?  I've started lurking on comp.lang.c++
> which seems to generate 100 msg a day or more.   It sort of reminds me of
> the last days of UCSD Pascal.  

> When is the last time you learned something about Modula-2 here?  Or
> anywhere?  Anyone care to try to talk me out of giving up on Modula-2 and
> learning C++?  (No arguments based on the intrinsic worth (not a pun) or
> M2's superior readability or design, please.  All I want to hear is a
> convincing argument that Modula-2 is not a dead language; that serious
> tools will be available for developing programmes for the major platforms
> for the rest of the decade (Win95, OS/2, workstations).

> Regards,

I echo your sentiments. It's difficult to pose this sort of question
without sounding cynical, but I believe it's a real problem. I fear
M2 is dying or dead.

I'd happily use M2 for virtually all of my tasks if I could get a compiler
(with good tools and ongoing support) for commercial operating systems
like the dreaded Windows.

I wonder what is the point of the (still unpublished) ISO standard. Will
it be completely pointless or will it cause a resurgence of interest in M2.

BUT.... don't go to C++ unless you are really desperate. Perhaps the answer
is Ada or Modula-3 or Oberon-2.

Eddie O'Brien



Fri, 08 Aug 1997 12:12:24 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:

> Is it time to give up on Modula-2?  I've started lurking on comp.lang.c++
> which seems to generate 100 msg a day or more.   It sort of reminds me of
> the last days of UCSD Pascal.  

This posting prompted me to go and take a look at comp.lang.c++.
It's true, there's a lot of traffic there.  But a very high
proportion of the postings relates to PROBLEMS with C++.
Difficulties in understand language features, questions of the
form "can anyone help me understand why this code doesn't work?",
questions about how to do things that are easier in other
languages, requests for help in understanding obscure error
messages, etc.

Quote:
> When is the last time you learned something about Modula-2 here?

Most of the questions that get asked in comp.lang.modula2 get
answered.  It's just that there aren't many questions.  Apparently
Modula-2 programmers are quietly getting things done, rather than
putting lots of time into trying to work out what went wrong.

I agree, there is a problem in terms of popularity of M2, and
I wish I could work out how to address it.  I just wanted to point
out that the amount of traffic in a newsgroup is not a good
indicator of whether a language is dying.

I can't work out whether all the noise in comp.lang.c++ comes
from birth pangs or death rattles; but it's certainly not a
sign of a healthy mature language.

--

                           file://ee.newcastle.edu.au/pub/www/Moylan.html



Sat, 09 Aug 1997 08:19:32 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:

>I can't work out whether all the noise in comp.lang.c++ comes
>from birth pangs or death rattles; but it's certainly not a
>sign of a healthy mature language.

There was an interesting comment in a recent c/c++ users group journal (the
paper one Plauger edits).  He was commenting about how every time the C++
ISO group meets they add a whole lot more to the language, and that this
was miaking life difficult.

I think when that started happening to ISO modula-2 we started helping to
kill M-2.  Maybe C++ will collapse under its own weight too.  But I fear
they have too much money tied up in it.  I wonder what will happen when
the reaction finally gets too big to ignore.



Sat, 09 Aug 1997 23:23:49 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

Quote:



> >I can't work out whether all the noise in comp.lang.c++ comes
> >from birth pangs or death rattles; but it's certainly not a
> >sign of a healthy mature language.

> There was an interesting comment in a recent c/c++ users group journal (the
> paper one Plauger edits).  He was commenting about how every time the C++
> ISO group meets they add a whole lot more to the language, and that this
> was miaking life difficult.

> I think when that started happening to ISO modula-2 we started helping to
> kill M-2.  Maybe C++ will collapse under its own weight too.  But I fear
> they have too much money tied up in it.  I wonder what will happen when
> the reaction finally gets too big to ignore.

I have a little different view of things, Pat.  Even with the additions
(most of which I think were well-conceived) our greatest damage was in not
being timely with the standard.  Much of that delay was caused by the
insistance of some that decisions made at previous meetings be revisited
again and again at subsequent meetings, eating up precious time, and
paralyzing the process.

Rick

--
Rick Sutcliffe    Assoc. Prof. Computing/Math   Trinity Western University
Canadian Chair, WG13. Not an official spokesperson for TWU or WG13



Sun, 10 Aug 1997 23:09:27 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR

| > Is it time to give up on Modula-2?  I've started lurking on comp.lang.c++
| > which seems to generate 100 msg a day or more.   It sort of reminds me of
| > the last days of UCSD Pascal.  

| This posting prompted me to go and take a look at comp.lang.c++.
| It's true, there's a lot of traffic there.  But a very high
| proportion of the postings relates to PROBLEMS with C++.

I would take it that this means there are a lot of new developers coming
onto the c++ scene, rather than there's some sort of inherent flaw (other
than being a really scary hack) with the language.

| Difficulties in understand language features, questions of the
| form "can anyone help me understand why this code doesn't work?",
| questions about how to do things that are easier in other
| languages, requests for help in understanding obscure error
| messages, etc.

Take it from me, m2 has its own set of obscure error messages. At least
c++ has the advantage that different compilers are likely to give
consistent error messages. You don't have to ask "are you using stoney-
brook or topspeed m2?" or even macMETH (I've just got it for my machine
today :)

| > When is the last time you learned something about Modula-2 here?

I did learn quite a bit on how to get the m2 code I'm writing for my
project to work with foreign modules - like stuff the rest of the world
uses... graphics libraries written in c or assembler f'rinstance.

| Most of the questions that get asked in comp.lang.modula2 get
| answered.  It's just that there aren't many questions.  Apparently
| Modula-2 programmers are quietly getting things done, rather than
| putting lots of time into trying to work out what went wrong.

Come on! The two biggest problems I've seen in the last couple of weeks
was a discussion on computational skill at uni and how do you pass a
matrix into a function. And no-one replied with the obvious answer to
the second problem (pass a pointer to the matrix guys - m2 is still barely
a real computer language).

| I agree, there is a problem in terms of popularity of M2, and
| I wish I could work out how to address it.  I just wanted to point
| out that the amount of traffic in a newsgroup is not a good
| indicator of whether a language is dying.

Uhh... yes it is. If no-one is having any problems then no-one is doing
anything new with the language. If there's no progession then obsolesence
starts creeping in. No-one developing means no commercial support, or even
enthusiastic amateur support.

| I can't work out whether all the noise in comp.lang.c++ comes
| from birth pangs or death rattles; but it's certainly not a
| sign of a healthy mature language.

c++ isn't going away. The corporate types have latched onto it as the
computing panacea for the next decade at least. All the folk I know who
develop in a corporate environment develop c++ (Visual C++ yeeeeuuuch!).

Ada's the only likely algol based contender for successor(?) to m2, and
with Ada 9x's OO extensions and stuff, maybe a few clones might get in on
the act and bring a bit on money into development of the language. I still
like C (although for work I've gotta use m2) and that was my little rant on
the subject.

Gary
--
Gary O'Keefe, College of Aeronautics, Cranfield University, Cranfield,
BEDS MK43 0AL | +44 123 475 0111 x 5207 | "War is deception" - Sun Tzu



Mon, 18 Aug 1997 03:56:44 GMT  
 Getting a CARDINAL into a zero-padded ARRAY OF CHAR


: >Please forgive the bone-headedness of this question: I'm a C programmer having
: >difficulties making the switch to M2.
: >
: >I have a value in a cardinal, say 13. I want to get this value into an array
: >of char formatted like "00013". How do I do this?
: >
: >Many thanks,
: >  Jeff.
: >
: >--

:       First, you must convert from number to string and then get
:       the string length. Then subtract from 5 (your implied string
:       length) this string length. Now you know how 0's you need to
:       prepend.  Fill a buffer (via a loop and concatenation) with
:       the required number of 0's and then concatenate the converted
:       string.

Just use ConstAssign("00000", NumString );
         Concat( NumString, IntToString( Int, Num, Conv ) );



Wed, 20 Aug 1997 08:33:32 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Zero-padding arrays

2. How to pad an array with zeros?

3. Padding Strings with zeros

4. CHAR with ARRAY OF CHAR assignment compatible ?

5. CHAR with ARRAY OF CHAR assignment compatible ?

6. zero padding

7. zero padded string

8. How to pad integer output with zeros

9. Shouldn't %06s zero-pad a string?

10. Array intrinsics with zero-size array argument

11. char array to float array routine

12. J Question: Unboxing arrays of strings without padding

 

 
Powered by phpBB® Forum Software