comp.lang.ada
 help / color / mirror / Atom feed
From: "Phaedrus" <phaedrusalt@hotmail.com>
Subject: Re: Binary value
Date: Sat, 31 Mar 2001 05:23:55 GMT
Date: 2001-03-31T05:23:55+00:00	[thread overview]
Message-ID: <LRdx6.11749$aP5.1074902@newsread2.prod.itd.earthlink.net> (raw)
In-Reply-To: K84x6.4066$MZ2.743519@news2-win.server.ntlworld.com

Yeah, I suppose you could do it with an algorithm.  But why bother?
The machine stores the values in binary, so just take advantage of the
fact.  Create a packed array of bits at the same location as the integer
to be converted.  Then just write the array out with a "for" loop.

It's not as easy as using the Put routine, but I like the elegance.  No muss,
no fuss, no bother.  By the way, this will also help you in doing the reverse,
where Text_io will leave you high and dry.   If you want to convert an inputted
binary number to base ten, once you've read it in it's no bother at all.  And
no work, too!

Phaedrus
"chris.danx" <chris.danx@ntlworld.com> wrote in message
news:K84x6.4066$MZ2.743519@news2-win.server.ntlworld.com...
> > I see... that answers half of my question. This presumes I wanna convert
> > from binary to Integer. But is it possible to "convert" a Integer value to
> > binary.
>
> Suppose you have the value 123 in binary.  To convert to binary you need to
> follow this algorithm.
>
> while num /= 0 loop
>     rem := num mod 2;    -- get remainder
>     num := num - rem;
>     add rem'image to front* of string;
> end loop;
>
> {I think this is the correct algorithm!}
>
> You could do it recursively too.
>
>
> Hope this helps,
> Chris Campbell
>
> *if you add rem'image onto the end you'll have to reverse the string after
> you finish so it'd be better to add it to the front.  I'm assuming you'll
> use an unbounded string in the loop and will convert it to a string outside
> if you wish.
>
>
>





  parent reply	other threads:[~2001-03-31  5:23 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-30 16:10 Binary value Bob Gratton
2001-03-30 16:54 ` Marin David Condic
2001-03-30 17:16   ` Bob Gratton
2001-03-30 17:41     ` Marin David Condic
2001-03-30 18:20       ` Bob Gratton
2001-03-30 19:58         ` Marin David Condic
2001-03-30 18:21     ` chris.danx
2001-03-30 20:03       ` Marin David Condic
2001-03-30 20:32       ` Smark
2001-03-30 20:45         ` Smark
2001-03-30 21:19           ` chris.danx
2001-03-31  5:23       ` Phaedrus [this message]
     [not found]         ` <3AC5C72F.8108A613@earthlink.net>
2001-04-01 19:52           ` Phaedrus
2001-04-01 22:04             ` Robert A Duff
2001-04-01 22:35               ` David C. Hoos, Sr.
2001-04-02 12:49               ` Marc A. Criley
2001-04-02 13:33             ` chris.danx
2001-04-02 14:33             ` Marin David Condic
2001-04-02 16:43       ` Jeffrey Carter
2001-04-02 17:36         ` chris.danx
2001-04-02 18:03           ` Robert A Duff
2001-04-02 17:54         ` Robert A Duff
2001-03-30 17:03 ` Larry Hazel
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox