* Binary value @ 2001-03-30 16:10 Bob Gratton 2001-03-30 16:54 ` Marin David Condic 2001-03-30 17:03 ` Larry Hazel 0 siblings, 2 replies; 23+ messages in thread From: Bob Gratton @ 2001-03-30 16:10 UTC (permalink / raw) Ok... now I know how to use bitwise operators... I'd like to know also how to convert an integer value to its equivalent in binary. e.g.: 123 => 1111011 Thanx again! Bob ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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:03 ` Larry Hazel 1 sibling, 1 reply; 23+ messages in thread From: Marin David Condic @ 2001-03-30 16:54 UTC (permalink / raw) It could not possibly get any easier. Ada allows you to express numeric literals in bases other than 10. The syntax is B#nnnn# where B is a base and the nnnn is the number in appropriate base representation. (octal, hex, binary,... base 7 or 5 if you want to be unique!) So a binary number could be expressed as: 2#1111_1111_1111_1111# or just for fun: 16#FFFF# 8#177777# 10#65535# IIRC, you can only use bases between 2..16 - but that covers most of the ground anybody would want. Note also that burried somewhere in Text_IO are routines that will allow you to print results using a base other than 10 - just in case you want to do a little I/O with your bits... MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Bob Gratton" <bobby@sympatico.ca> wrote in message news:9e2x6.568987$JT5.15730002@news20.bellglobal.com... > Ok... now I know how to use bitwise operators... I'd like to know also how > to convert an integer value to its equivalent in binary. > > e.g.: 123 => 1111011 > > Thanx again! > > Bob > > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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:21 ` chris.danx 0 siblings, 2 replies; 23+ messages in thread From: Bob Gratton @ 2001-03-30 17:16 UTC (permalink / raw) [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1: Type: text/plain, Size: 1647 bytes --] 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. For example, if one of my program's users enters a value, how can I show him the conversion in binary? "Marin David Condic" <marin.condic.auntie.spam@pacemicro.com> a �crit dans le message news: 9a2dnu$sff$1@nh.pace.co.uk... > It could not possibly get any easier. Ada allows you to express numeric > literals in bases other than 10. The syntax is B#nnnn# where B is a base and > the nnnn is the number in appropriate base representation. (octal, hex, > binary,... base 7 or 5 if you want to be unique!) So a binary number could > be expressed as: > > 2#1111_1111_1111_1111# > > or just for fun: > > 16#FFFF# > 8#177777# > 10#65535# > > IIRC, you can only use bases between 2..16 - but that covers most of the > ground anybody would want. > > Note also that burried somewhere in Text_IO are routines that will allow you > to print results using a base other than 10 - just in case you want to do a > little I/O with your bits... > > MDC > -- > Marin David Condic > Senior Software Engineer > Pace Micro Technology Americas www.pacemicro.com > Enabling the digital revolution > e-Mail: marin.condic@pacemicro.com > Web: http://www.mcondic.com/ > > > "Bob Gratton" <bobby@sympatico.ca> wrote in message > news:9e2x6.568987$JT5.15730002@news20.bellglobal.com... > > Ok... now I know how to use bitwise operators... I'd like to know also how > > to convert an integer value to its equivalent in binary. > > > > e.g.: 123 => 1111011 > > > > Thanx again! > > > > Bob > > > > > > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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 18:21 ` chris.danx 1 sibling, 1 reply; 23+ messages in thread From: Marin David Condic @ 2001-03-30 17:41 UTC (permalink / raw) [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1: Type: text/plain, Size: 3867 bytes --] As I said, you have features in Text_IO to display data in forms other than base 10. Step 1: Bookmark this site: http://www.adapower.org/ Step 2: Click on the "Reference" button and select "On-line version of the Ada 95 Reference Manual at AdaPower.com" Step 3: Find: Appendix A.10.8, wherein you will find documentation on the "Put" procedure. (Check out all the other cool stuff while you're there, like the various Ada.Strings... packages, etc. Lots of useful tools for you there.) A copy here for your convenience: ==================================================== procedure Put(File : in File_Type; Item : in Num; Width : in Field := Default_Width; Base : in Number_Base := Default_Base); procedure Put(Item : in Num; Width : in Field := Default_Width; Base : in Number_Base := Default_Base); Outputs the value of the parameter Item as an integer literal, with no low lines, no exponent, and no leading zeros (but a single zero for the value zero), and a preceding minus sign for a negative value. If the resulting sequence of characters to be output has fewer than Width characters, then leading spaces are first output to make up the difference. Uses the syntax for decimal literal if the parameter Base has the value ten (either explicitly or through Default_Base); otherwise, uses the syntax for based literal, with any letters in upper case. ==================================================== If you intend to do very much programming in Ada at all, you'll want to get nice and comfortable with the ARM. Check out AdaPower as a really first rate resource - especially the on-line version of the ARM. (BTW: Many compilers ship with some version of the ARM available to you.) Hope this helps. MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Bob Gratton" <bobby@sympatico.ca> wrote in message news:ub3x6.569521$JT5.15735623@news20.bellglobal.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. For example, if one of my program's users enters a value, how can I > show him the conversion in binary? > > "Marin David Condic" <marin.condic.auntie.spam@pacemicro.com> a �crit dans > le message news: 9a2dnu$sff$1@nh.pace.co.uk... > > It could not possibly get any easier. Ada allows you to express numeric > > literals in bases other than 10. The syntax is B#nnnn# where B is a base > and > > the nnnn is the number in appropriate base representation. (octal, hex, > > binary,... base 7 or 5 if you want to be unique!) So a binary number could > > be expressed as: > > > > 2#1111_1111_1111_1111# > > > > or just for fun: > > > > 16#FFFF# > > 8#177777# > > 10#65535# > > > > IIRC, you can only use bases between 2..16 - but that covers most of the > > ground anybody would want. > > > > Note also that burried somewhere in Text_IO are routines that will allow > you > > to print results using a base other than 10 - just in case you want to do > a > > little I/O with your bits... > > > > MDC > > -- > > Marin David Condic > > Senior Software Engineer > > Pace Micro Technology Americas www.pacemicro.com > > Enabling the digital revolution > > e-Mail: marin.condic@pacemicro.com > > Web: http://www.mcondic.com/ > > > > > > "Bob Gratton" <bobby@sympatico.ca> wrote in message > > news:9e2x6.568987$JT5.15730002@news20.bellglobal.com... > > > Ok... now I know how to use bitwise operators... I'd like to know also > how > > > to convert an integer value to its equivalent in binary. > > > > > > e.g.: 123 => 1111011 > > > > > > Thanx again! > > > > > > Bob > > > > > > > > > > > > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 17:41 ` Marin David Condic @ 2001-03-30 18:20 ` Bob Gratton 2001-03-30 19:58 ` Marin David Condic 0 siblings, 1 reply; 23+ messages in thread From: Bob Gratton @ 2001-03-30 18:20 UTC (permalink / raw) [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1: Type: text/plain, Size: 4432 bytes --] GEE!!! Thanx a lot! You cannot guess how much this will help... I should send you a check! My VISA number! A box of chocolate chip cookies! I'm just beginning in Ada, but you just gave me the tools I needed... Thanx again! "Marin David Condic" <marin.condic.auntie.spam@pacemicro.com> a �crit dans le message news: 9a2gge$6m$1@nh.pace.co.uk... > As I said, you have features in Text_IO to display data in forms other than > base 10. > > Step 1: Bookmark this site: http://www.adapower.org/ > Step 2: Click on the "Reference" button and select "On-line version of the > Ada 95 Reference Manual at AdaPower.com" > Step 3: Find: Appendix A.10.8, wherein you will find documentation on the > "Put" procedure. (Check out all the other cool stuff while you're there, > like the various Ada.Strings... packages, etc. Lots of useful tools for you > there.) A copy here for your convenience: > > ==================================================== > procedure Put(File : in File_Type; > Item : in Num; > Width : in Field := Default_Width; > Base : in Number_Base := Default_Base); > > procedure Put(Item : in Num; > Width : in Field := Default_Width; > Base : in Number_Base := Default_Base); > > Outputs the value of the parameter Item as an integer literal, with no low > lines, no exponent, and no leading zeros (but a single zero for the value > zero), and a preceding minus sign for a negative value. > If the resulting sequence of characters to be output has fewer than Width > characters, then leading spaces are first output to make up the difference. > Uses the syntax for decimal literal if the parameter Base has the value ten > (either explicitly or through Default_Base); otherwise, uses the syntax for > based literal, with any letters in upper case. > ==================================================== > > If you intend to do very much programming in Ada at all, you'll want to get > nice and comfortable with the ARM. Check out AdaPower as a really first rate > resource - especially the on-line version of the ARM. (BTW: Many compilers > ship with some version of the ARM available to you.) > > Hope this helps. > > MDC > -- > Marin David Condic > Senior Software Engineer > Pace Micro Technology Americas www.pacemicro.com > Enabling the digital revolution > e-Mail: marin.condic@pacemicro.com > Web: http://www.mcondic.com/ > > > "Bob Gratton" <bobby@sympatico.ca> wrote in message > news:ub3x6.569521$JT5.15735623@news20.bellglobal.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. For example, if one of my program's users enters a value, how can > I > > show him the conversion in binary? > > > > "Marin David Condic" <marin.condic.auntie.spam@pacemicro.com> a �crit dans > > le message news: 9a2dnu$sff$1@nh.pace.co.uk... > > > It could not possibly get any easier. Ada allows you to express numeric > > > literals in bases other than 10. The syntax is B#nnnn# where B is a base > > and > > > the nnnn is the number in appropriate base representation. (octal, hex, > > > binary,... base 7 or 5 if you want to be unique!) So a binary number > could > > > be expressed as: > > > > > > 2#1111_1111_1111_1111# > > > > > > or just for fun: > > > > > > 16#FFFF# > > > 8#177777# > > > 10#65535# > > > > > > IIRC, you can only use bases between 2..16 - but that covers most of the > > > ground anybody would want. > > > > > > Note also that burried somewhere in Text_IO are routines that will allow > > you > > > to print results using a base other than 10 - just in case you want to > do > > a > > > little I/O with your bits... > > > > > > MDC > > > -- > > > Marin David Condic > > > Senior Software Engineer > > > Pace Micro Technology Americas www.pacemicro.com > > > Enabling the digital revolution > > > e-Mail: marin.condic@pacemicro.com > > > Web: http://www.mcondic.com/ > > > > > > > > > "Bob Gratton" <bobby@sympatico.ca> wrote in message > > > news:9e2x6.568987$JT5.15730002@news20.bellglobal.com... > > > > Ok... now I know how to use bitwise operators... I'd like to know also > > how > > > > to convert an integer value to its equivalent in binary. > > > > > > > > e.g.: 123 => 1111011 > > > > > > > > Thanx again! > > > > > > > > Bob > > > > > > > > > > > > > > > > > > > > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 18:20 ` Bob Gratton @ 2001-03-30 19:58 ` Marin David Condic 0 siblings, 0 replies; 23+ messages in thread From: Marin David Condic @ 2001-03-30 19:58 UTC (permalink / raw) Sucking up will get you all kinds of additional help. :-) (BTW: You forgot to mention what an obvious Wizard I am as well as my clearly abundant Humanitarian tendancies.) Seriously: You will discover that Ada has just about all the clever facilities you need to get almost any job done. You just have to be either very familiar with the ARM and/or be good at digging around on the net a little to find free Ada sources generously put there by her numerous admirers and advocates. MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Bob Gratton" <bobby@sympatico.ca> wrote in message news:I74x6.569791$JT5.15739046@news20.bellglobal.com... > GEE!!! Thanx a lot! You cannot guess how much this will help... > I should send you a check! My VISA number! A box of chocolate chip cookies! > I'm just beginning in Ada, but you just gave me the tools I needed... > > Thanx again! > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 17:16 ` Bob Gratton 2001-03-30 17:41 ` Marin David Condic @ 2001-03-30 18:21 ` chris.danx 2001-03-30 20:03 ` Marin David Condic ` (3 more replies) 1 sibling, 4 replies; 23+ messages in thread From: chris.danx @ 2001-03-30 18:21 UTC (permalink / raw) > 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. ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 18:21 ` chris.danx @ 2001-03-30 20:03 ` Marin David Condic 2001-03-30 20:32 ` Smark ` (2 subsequent siblings) 3 siblings, 0 replies; 23+ messages in thread From: Marin David Condic @ 2001-03-30 20:03 UTC (permalink / raw) It is *MUCH* easier simply to look at what is available to you in Text_IO. From ARM A.10.8: ============================================= procedure Put(To : out String; Item : in Num; Base : in Number_Base := Default_Base); Outputs the value of the parameter Item to the given string, following the same rule as for output to a file, using the length of the given string as the value for Width. ================================================ This will put an integer value in any base you like (almost!) into a string. Similar stuff exists for other numeric types. MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "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. > > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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-31 5:23 ` Phaedrus 2001-04-02 16:43 ` Jeffrey Carter 3 siblings, 1 reply; 23+ messages in thread From: Smark @ 2001-03-30 20:32 UTC (permalink / raw) "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!} Well, Chris, you would probably change your mind if you tried it! As in your example, say you start with 123. First pass through loop: rem := 123 mod 2 ( = 1) num := 123 - 1 ( = 122) add "1" to front of string -- so the string now looks like "1" Second pass: rem := 122 mod 2 ( = 0) num := 122 - 0 ( = 122) add "0" to front of string -- so the string now looks like "01" Third pass: rem := 122 mod 2 ( = 0) num := 122 - 0 ( = 122) add "0" to front of string -- so the string now looks like "001" Beginning to see a problem here? I think the algorithm you were trying to think of goes something like: Rem := Num; Exp := 31; Index := 1; while Exp >= 0 loop Check := 2 ** Exp; if Rem >= Check then Bit_String(Index) := '1'; else Bit_String(Index) := '0'; end if; Rem := Rem - Check; Exp := Exp - 1; Index := Index + 1; end loop; However, there are much easier ways. You should take a look at the facilities provided in Text_Io, as Marin mentioned. Also, if you want to do something with the bits other than display them, you could do an unchecked_conversion to a packed bit array of the same size as the number. Regards, Mark Arlington, Texas ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 20:32 ` Smark @ 2001-03-30 20:45 ` Smark 2001-03-30 21:19 ` chris.danx 0 siblings, 1 reply; 23+ messages in thread From: Smark @ 2001-03-30 20:45 UTC (permalink / raw) "Smark" <nobody@home.com> wrote in message news:9a2qhh$jee4@cui1.lmms.lmco.com... > > "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!} > > Well, Chris, you would probably change your mind if you tried it! > As in your example, say you start with 123. > > First pass through loop: > rem := 123 mod 2 ( = 1) > num := 123 - 1 ( = 122) > add "1" to front of string -- so the string now looks like "1" > > Second pass: > rem := 122 mod 2 ( = 0) > num := 122 - 0 ( = 122) > add "0" to front of string -- so the string now looks like "01" > > Third pass: > rem := 122 mod 2 ( = 0) > num := 122 - 0 ( = 122) > add "0" to front of string -- so the string now looks like "001" > > Beginning to see a problem here? > > I think the algorithm you were trying to think of goes something like: > Okay, I take it back. The algorithm I provided does work, but it is not the one you were thinking of. That would be more like: while num /= 0 loop rem := num mod 2; -- get remainder num := Floor(num/2); add rem'image to front* of string; end loop; Mark Arlington, Texas ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 20:45 ` Smark @ 2001-03-30 21:19 ` chris.danx 0 siblings, 0 replies; 23+ messages in thread From: chris.danx @ 2001-03-30 21:19 UTC (permalink / raw) I forgot to divide by 2, that's silly. so it should have been num := (num - rem)/2; I'll remember that next time, Thanks Chris Campbell ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 18:21 ` chris.danx 2001-03-30 20:03 ` Marin David Condic 2001-03-30 20:32 ` Smark @ 2001-03-31 5:23 ` Phaedrus [not found] ` <3AC5C72F.8108A613@earthlink.net> 2001-04-02 16:43 ` Jeffrey Carter 3 siblings, 1 reply; 23+ messages in thread From: Phaedrus @ 2001-03-31 5:23 UTC (permalink / raw) 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. > > > ^ permalink raw reply [flat|nested] 23+ messages in thread
[parent not found: <3AC5C72F.8108A613@earthlink.net>]
* Re: Binary value [not found] ` <3AC5C72F.8108A613@earthlink.net> @ 2001-04-01 19:52 ` Phaedrus 2001-04-01 22:04 ` Robert A Duff ` (2 more replies) 0 siblings, 3 replies; 23+ messages in thread From: Phaedrus @ 2001-04-01 19:52 UTC (permalink / raw) > > 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" <mcqada@earthlink.net> 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 > $ > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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 2 siblings, 2 replies; 23+ messages in thread From: Robert A Duff @ 2001-04-01 22:04 UTC (permalink / raw) "Phaedrus" <phaedrusalt@hotmail.com> writes: > 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. You can read the thing as a string, and then say something like T'Image("2#" & The_Input_String & "#"). - Bob ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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 1 sibling, 0 replies; 23+ messages in thread From: David C. Hoos, Sr. @ 2001-04-01 22:35 UTC (permalink / raw) Don't you mean T'Value ("2#" & The_Input_String & "#") instead of T'Image ? . "Robert A Duff" <bobduff@world.std.com> wrote in message news:wccr8zc2s5v.fsf@world.std.com... > "Phaedrus" <phaedrusalt@hotmail.com> writes: > > > 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. > > You can read the thing as a string, and then say something like > T'Image("2#" & The_Input_String & "#"). > > - Bob ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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 1 sibling, 0 replies; 23+ messages in thread From: Marc A. Criley @ 2001-04-02 12:49 UTC (permalink / raw) Robert A Duff wrote: > > "Phaedrus" <phaedrusalt@hotmail.com> writes: > > > 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. > > You can read the thing as a string, and then say something like > T'Image("2#" & The_Input_String & "#"). Yeah, I would either do this (except use 'Value instead of 'Image, like David mentioned :-), or Get from a string. I rarely do direct numeric input, especially if it's interactive. I use Get_Line, which gives me a chance to more simply handle things like C/Rs or do some kind of input validation, and then extract the value from that string. Marc ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-04-01 19:52 ` Phaedrus 2001-04-01 22:04 ` Robert A Duff @ 2001-04-02 13:33 ` chris.danx 2001-04-02 14:33 ` Marin David Condic 2 siblings, 0 replies; 23+ messages in thread From: chris.danx @ 2001-04-02 13:33 UTC (permalink / raw) What about a routine that takes a string with a O,B,D,H at the end to identify the base. e.g. 101B 101D 101H 101O maybe this'd be difficult to do but you could put a separator between number and Base. 101:B for example Just a thought, Chris Campbell ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-04-01 19:52 ` Phaedrus 2001-04-01 22:04 ` Robert A Duff 2001-04-02 13:33 ` chris.danx @ 2001-04-02 14:33 ` Marin David Condic 2 siblings, 0 replies; 23+ messages in thread From: Marin David Condic @ 2001-04-02 14:33 UTC (permalink / raw) Here's a trick that might not have occurred to you: You want to set a "default base" and let the user type in a number like that without having to put in all the dingleberries around the digits. (Digits in this case is a misnomer. What is the word I want?) Prefix : constant String := "16#" ; Suffix : constant String := "#" ; .... Text_IO.Get_Line (Some_String, Last) ; .... Integer_Text_IO.Get ( From => Prefix & Some_String (1..Last) & Suffix, Item => Some_Integer, Last => Last) ; .... Naturally, you can do some setup with the prefix & suffix strings to make them selectable by the user at runtime. In the end, you probably want to read numbers in as strings anyway because you will want to create more forgiving editing than you customarily get immediately from Ada. (Quite often a response can be a number of things besides a number. Picture in your mind: "Enter the number or type 'Exit' to go back or 'Quit' to terminate...") Just another way of removing the epidermis from the household feline. MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Phaedrus" <phaedrusalt@hotmail.com> wrote in message news:2GLx6.16115$aP5.1507862@newsread2.prod.itd.earthlink.net... > 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. > ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 18:21 ` chris.danx ` (2 preceding siblings ...) 2001-03-31 5:23 ` Phaedrus @ 2001-04-02 16:43 ` Jeffrey Carter 2001-04-02 17:36 ` chris.danx 2001-04-02 17:54 ` Robert A Duff 3 siblings, 2 replies; 23+ messages in thread From: Jeffrey Carter @ 2001-04-02 16:43 UTC (permalink / raw) "chris.danx" wrote: > > > 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. > [incorrect algorithm deleted] Ignoring the question of what the correct algorithm is, why would you want to implement this (specific to binary) algorithm, when Text_IO already provides this capability for any base from 2 to 16? It's much easier and more flexible to use Text_IO.Integer_IO.Put and extract the part of the result between the '#'s. Jeffrey Carter ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 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 1 sibling, 1 reply; 23+ messages in thread From: chris.danx @ 2001-04-02 17:36 UTC (permalink / raw) > [incorrect algorithm deleted] one mistake made in haste! I appologise! > Ignoring the question of what the correct algorithm is, why would you > want to implement this (specific to binary) algorithm, when Text_IO > already provides this capability for any base from 2 to 16? It's much > easier and more flexible to use Text_IO.Integer_IO.Put and extract the > part of the result between the '#'s. Guess it's just me. This is how we done conversions in school and it was the first thing that popped into my head. You're right i should provide a 2 to 16 base feature which would just be slight modification (with added complications for base > 10). Chris ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-04-02 17:36 ` chris.danx @ 2001-04-02 18:03 ` Robert A Duff 0 siblings, 0 replies; 23+ messages in thread From: Robert A Duff @ 2001-04-02 18:03 UTC (permalink / raw) "chris.danx" <chris.danx@ntlworld.com> writes: > one mistake made in haste! I appologise! Don't apologize -- it was very instructive. You made Jeffrey Carter's point quite nicely, which was that you should use existing code that works, if you can. ;-) ;-) - Bob ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-04-02 16:43 ` Jeffrey Carter 2001-04-02 17:36 ` chris.danx @ 2001-04-02 17:54 ` Robert A Duff 1 sibling, 0 replies; 23+ messages in thread From: Robert A Duff @ 2001-04-02 17:54 UTC (permalink / raw) Jeffrey Carter <jeffrey.carter@boeing.com> writes: > Ignoring the question of what the correct algorithm is, why would you > want to implement this (specific to binary) algorithm, when Text_IO > already provides this capability for any base from 2 to 16? It's much > easier and more flexible to use Text_IO.Integer_IO.Put and extract the > part of the result between the '#'s. Good point, but I can think of a couple of reasons why someone might want to avoid using Text_IO at all: it's big (which matters if you care about space, and don't have a clever linker), and it's not pragma-Pure. I'd recommend 'Image and 'Value rather than Text_IO, even though they have their faults, too. - Bob ^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: Binary value 2001-03-30 16:10 Binary value Bob Gratton 2001-03-30 16:54 ` Marin David Condic @ 2001-03-30 17:03 ` Larry Hazel 1 sibling, 0 replies; 23+ messages in thread From: Larry Hazel @ 2001-03-30 17:03 UTC (permalink / raw) Bob Gratton wrote: > > Ok... now I know how to use bitwise operators... I'd like to know also how > to convert an integer value to its equivalent in binary. > > e.g.: 123 => 1111011 > > Thanx again! > > Bob To write a binary constant use 2#1111011#, if I remember correctly. Larry ^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2001-04-02 18:03 UTC | newest] Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 [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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox