From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a96b706fab1e8c2c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-01 12:54:02 PST Path: supernews.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!newsfeed.direct.ca!look.ca!newsfeed1.earthlink.net!newsfeed.earthlink.net!newsmaster1.prod.itd.earthlink.net!newsread2.prod.itd.earthlink.net.POSTED!not-for-mail From: "Phaedrus" Newsgroups: comp.lang.ada References: <9e2x6.568987$JT5.15730002@news20.bellglobal.com> <9a2dnu$sff$1@nh.pace.co.uk> <3AC5C72F.8108A613@earthlink.net> Subject: Re: Binary value X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Message-ID: <2GLx6.16115$aP5.1507862@newsread2.prod.itd.earthlink.net> Date: Sun, 01 Apr 2001 19:52:30 GMT NNTP-Posting-Host: 209.178.103.28 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 986154750 209.178.103.28 (Sun, 01 Apr 2001 12:52:30 PDT) NNTP-Posting-Date: Sun, 01 Apr 2001 12:52:30 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: supernews.google.com comp.lang.ada:6319 Date: 2001-04-01T19:52:30+00:00 List-Id: > > 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! > > Hunh?? I've often thought that the "default base" in text_io should allow you to set the default INPUT base, too, so that users of it could just specify that they wanted the user to be able to plug in a number in a certain base without using the base and pound signs. Unfortunately, it doesn't work that way. > To convert an inputted binary number to base ten, simply enter it as a > binary number. If the program says "Get(N)", you can legally provide > 38, 2#1001#, 16#DEADBEEF#, etc. True, and it works great, except that the base and pound signs are sort of a pain. If you were going to enter, say, 30 or 40 binary numbers, each consisting of 8 or more digits, orif you're writing a utility to be used by less-than-advanced users, who might balk at writing the base and the pound signs, or to convert binary data that is input as an ASCII stream by an external device.... I can think of many situations where the user might object to typing in the base and pound signs. (For instance, what if the calculator in windows required you to put the base and pounds around a number? Who'd use it?) In that case, you're pretty much left with using a loop to get the number. And once you've got it, it's kind of cool to be able to convert it without any calculation. I really love the look on people's faces when they ask to see the algorithm for verification! "There isn't one" isn't an answer they're used to hearing. Phaedrus "Marc A. Criley" wrote in message news:3AC5C72F.8108A613@earthlink.net... > Phaedrus wrote: > > > > 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. > > When you've got Put, why bother with packed arrays and so on? > > Put(N, Base => 2); -- Done! > > > > > 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! > > Hunh?? > > To convert an inputted binary number to base ten, simply enter it as a > binary number. If the program says "Get(N)", you can legally provide > 38, 2#1001#, 16#DEADBEEF#, etc. > > ------------------------------------------------------------- > > with Ada.Integer_Text_Io; > use Ada.Integer_Text_Io; > with Text_IO; > use Text_IO; > > procedure Binary_IO is > > N : Integer; > > begin > loop > Put("Enter # (0 to exit): "); > Get(N); > exit when N = 0; > Put("As base 2: "); > Put(N, Base => 2); > New_Line; > Put("As base 8: "); > Put(N, Base => 8); > New_Line; > Put("As base 13: "); > Put(N, Base => 13); > New_Line; > Put("As base 16: "); > Put(N, Base => 16); > New_Line(2); > end loop; > end Binary_IO; > > Here's a sample run: > > $ binary_io > Enter # (0 to exit): 20 > As base 2: 2#10100# > As base 8: 8#24# > As base 13: 13#17# > As base 16: 16#14# > > Enter # (0 to exit): 2#1101# > As base 2: 2#1101# > As base 8: 8#15# > As base 13: 13#10# > As base 16: 16#D# > > Enter # (0 to exit): 16#ABCD# > As base 2: 2#1010101111001101# > As base 8: 8#125715# > As base 13: 13#17032# > As base 16: 16#ABCD# > > Enter # (0 to exit): 0 > $ >