* String to Integer @ 1997-07-15 0:00 Charles Hill 1997-07-15 0:00 ` John Herro ` (4 more replies) 0 siblings, 5 replies; 10+ messages in thread From: Charles Hill @ 1997-07-15 0:00 UTC (permalink / raw) I have a string in which an integer is stored. I'd like to hear from someone who might know a convenient way to convert this to an actual integer. I'm wondering if there is a function built into one of the Ada packages to do this, but I haven't found one yet. I would appreciate any advice anyone has. Thanks, -Brian Hill -- Charles Brian Hill Florida State University hill@cs.fsu.edu Computer Science Department ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: String to Integer 1997-07-15 0:00 String to Integer Charles Hill @ 1997-07-15 0:00 ` John Herro 1997-07-16 0:00 ` Samuel Mize ` (3 subsequent siblings) 4 siblings, 0 replies; 10+ messages in thread From: John Herro @ 1997-07-15 0:00 UTC (permalink / raw) hill@cs.fsu.edu (Charles Hill) writes: > I have a string in which an integer is stored. > I'd like to ... convert this to an actual integer. > I'm wondering if there is a function built into > one of the Ada packages to do this... Not into an Ada *package*, but built into the language itself. If S is the string and I is an Integer variable, you can write I := Integer'Value(S); The 'Value is called an attribute. The 'Image attribute goes the other way, so you could also write S := Integer'Image(I); 'Image and 'Value work with many types, not just Integer, but they always convert to and from strings. For more information, you can download the Ada Tutor program from the Web or FTP sites below my signature. I hope this helps. - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: String to Integer 1997-07-15 0:00 String to Integer Charles Hill 1997-07-15 0:00 ` John Herro @ 1997-07-16 0:00 ` Samuel Mize 1997-07-16 0:00 ` Robert Dewar ` (2 subsequent siblings) 4 siblings, 0 replies; 10+ messages in thread From: Samuel Mize @ 1997-07-16 0:00 UTC (permalink / raw) Charles Hill wrote: > > I have a string in which an integer is stored. I'd like to hear > from someone who might know a convenient way to convert this to > an actual integer. I'm wondering if there is a function built > into one of the Ada packages to do this, but I haven't found one > yet. > > I would appreciate any advice anyone has. Do you mean that the string contains the characters that represent the number, or that the string storage is also used as binary storage for an integer? In the first case (e.g. "301") you can use integer'image and integer'value to translate between the string and the integer value. If your string is a buffer that may have garbage after the end of the number (e.g., a null-terminated varying-length string in a fixed-length buffer), it may be simpler to use Ada.Text_Io.Integer_Io.Get from string, since it stops at the first non-numeric character (while Integer'Value would try to translate the entire string, and fail if there is anything in it but the integer and blanks). If you have a binary representation of an integer in your string, you should use unchecked_conversion. Samuel Mize ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: String to Integer 1997-07-15 0:00 String to Integer Charles Hill 1997-07-15 0:00 ` John Herro 1997-07-16 0:00 ` Samuel Mize @ 1997-07-16 0:00 ` Robert Dewar 1997-07-17 0:00 ` Embedding Text Data in Source (Was Re: String to Integer) John M. Mills 1997-07-16 0:00 ` String to Integer Martin C. Carlisle 1997-07-17 0:00 ` Michael F Brenner 4 siblings, 1 reply; 10+ messages in thread From: Robert Dewar @ 1997-07-16 0:00 UTC (permalink / raw) Charles says <<I have a string in which an integer is stored. I'd like to hear from someone who might know a convenient way to convert this to an actual integer. I'm wondering if there is a function built into one of the Ada packages to do this, but I haven't found one yet. > A good piece of advice for beginning Ada programmers is to read through the list of attributes in the back of the RM. You won't understand them all, but you will dig up a number of useful things that wil otherwise escape your attention (e.g. min, max, and in this particular case, value, which will do the trick for you). You can of course also use Text_IO's read from string. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Embedding Text Data in Source (Was Re: String to Integer) 1997-07-16 0:00 ` Robert Dewar @ 1997-07-17 0:00 ` John M. Mills 1997-07-17 0:00 ` Samuel T. Harris 1997-07-19 0:00 ` Matthew Heaney 0 siblings, 2 replies; 10+ messages in thread From: John M. Mills @ 1997-07-17 0:00 UTC (permalink / raw) I have a question which diverges slightly from the original topic, but is somewhat related. Please redirect me to "Read The Fine Manual" if that is appropriate, but be specific if you can. I now provide object parameters for a program in the form of a number of small files whose names are passed to the <ObjectType1>.Create(ParamFileName1, ...) procedures for each of the objects in my model. This works fine. I would also like to have a set of default parameters compiled into the code. Hard- coding this would be simple, but I want instead to include as the default a specified file from my source tree, verbatim, then use the same parsing routines to process either the file image embedded in the code or one read under a file name, as transparently as possible using approaches similar to the earlier suggestions in this thread: String'Value( ParamStr ) ... etc. The present form of my parameter files is similar to this: ! comment description VersionIDString ! string to match for error checking ! comment lines as desired RealValueString ! parameter value ! descriptive comment(s) IntegerValueString ! parameter value EnumValueString ! and so forth, with '*ValueString's in fixed order ... There is no guarantee that a line will have a specific length, but the sequence and grouping of parameters by [non-comment] line is well-defined. THE QUESTION: How can I embed those files' contents in my code automatically at compilation so they can be parsed as strings? Hope I managed to explain that .. &:^P I am using GNAT on a variety of UNIX platforms. Thanks -- john mills -- -- John M. Mills, Senior Research Engineer -- john.m.mills@gtri.gatech.edu Georgia Tech Research Institute, Georgia Tech, Atlanta, GA 30332-0834 Phone contacts: 404.894.0151 (voice), 404.894.6258 (FAX) "The 'Quick and Dirty' approach can generally be relied on to be dirty."-RlF ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Embedding Text Data in Source (Was Re: String to Integer) 1997-07-17 0:00 ` Embedding Text Data in Source (Was Re: String to Integer) John M. Mills @ 1997-07-17 0:00 ` Samuel T. Harris 1997-07-19 0:00 ` Matthew Heaney 1 sibling, 0 replies; 10+ messages in thread From: Samuel T. Harris @ 1997-07-17 0:00 UTC (permalink / raw) John M. Mills wrote: > > I have a question which diverges slightly from the original topic, but is > somewhat related. Please redirect me to "Read The Fine Manual" if that > is appropriate, but be specific if you can. > > I now provide object parameters for a program in the form of a number of small > files whose names are passed to the <ObjectType1>.Create(ParamFileName1, ...) > procedures for each of the objects in my model. This works fine. I would > also like to have a set of default parameters compiled into the code. Hard- > coding this would be simple, but I want instead to include as the default > a specified file from my source tree, verbatim, then use the same parsing > routines to process either the file image embedded in the code or one read > under a file name, as transparently as possible using approaches similar to > the earlier suggestions in this thread: String'Value( ParamStr ) ... etc. > > The present form of my parameter files is similar to this: > > ! comment description > VersionIDString ! string to match for error checking > ! comment lines as desired > RealValueString ! parameter value > ! descriptive comment(s) > IntegerValueString ! parameter value > EnumValueString ! and so forth, with '*ValueString's in fixed order > ... > > There is no guarantee that a line will have a specific length, but the > sequence and grouping of parameters by [non-comment] line is well-defined. > > THE QUESTION: > How can I embed those files' contents in my code automatically at compilation > so they can be parsed as strings? > > Hope I managed to explain that .. &:^P I am using GNAT on a variety of > UNIX platforms. > > Thanks -- john mills -- > > -- > John M. Mills, Senior Research Engineer -- john.m.mills@gtri.gatech.edu > Georgia Tech Research Institute, Georgia Tech, Atlanta, GA 30332-0834 > Phone contacts: 404.894.0151 (voice), 404.894.6258 (FAX) > "The 'Quick and Dirty' approach can generally be relied on to be dirty."-RlF My personal coding standard has long included producing an image and value function for any interesting aggregate (record or array) type and these functions work with strings which appear as Ada aggregates. This way I can initialize such objects with aggregates in the code, or copy the aggregate source itself to a data file which I read at runtime. I find the interoperability of the format to be a great time saver. I use functions parse and format when I need a syntax which differs from Ada aggregate syntax and I also sometimes provide a function debug which produces a string with any extra intrernal debugging info I might wish to include. When using image/value, I can use the original source code aggregates for my data file, and vice versa. No problem there. What is in a particular data file just becomes the source for an assignment statement. Cut and Paste. When using parse/format (as John would need since his syntax differs from Ada aggregates) I need filtering utilities to convert the strings. Since the syntax differs, I cannot use the data file in a direct assignment statement. I have to state the text of the data file in a string and parse that string. The filtering involved is not very complicated. 1. Replace existing double quotes (") with two double quotes (""). 2. Prefix each line with a double quote ("). 3. Suffix each line with a double quote ("). 4. Add the following to each line except the last line ... & ascii.lf & ... if you wish to perserve the newlines implied in the file, which is usually the case since the end of a line is often an important delimiter. 5. You are done. The processed text can be inserted into your Ada code as an source for a initialization of a string object (or constant string object if you are using Ada83). Note that such a string should be hidden inside a initialization procedure and not made global to a package. If global, it will consume memory long after your program has no further need for it. By staging it inside a procedure, it will (usually) be placed on the stack as the procedure starts, and then clean away when the procedure exits (and after you have processed it). Filtering from such processed text in you Ada source back into a data file involves performing the inverse steps in reverse order. Processing John's example gives ... init_string : constant string := "! comment description" & ascii.lf & "VersionIDString ! string to match for error checking" & ascii.lf & "! comment lines as desired" & ascii.lf & "RealValueString ! parameter value" & ascii.lf & "! descriptive comment(s)" & ascii.lf & "IntegerValueString ! parameter value" & ascii.lf & "EnumValueString ! and so forth, with '*ValueString's in fixed order" ; -- Samuel T. Harris, Senior Engineer Hughes Training, Inc. - Houston Operations 2224 Bay Area Blvd. Houston, TX 77058-2099 "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Embedding Text Data in Source (Was Re: String to Integer) 1997-07-17 0:00 ` Embedding Text Data in Source (Was Re: String to Integer) John M. Mills 1997-07-17 0:00 ` Samuel T. Harris @ 1997-07-19 0:00 ` Matthew Heaney 1 sibling, 0 replies; 10+ messages in thread From: Matthew Heaney @ 1997-07-19 0:00 UTC (permalink / raw) In article <5ql62v$8dp@acmey.gatech.edu>, jm59@prism.gatech.edu (John M. Mills) wrote: >I now provide object parameters for a program in the form of a number of small >files whose names are passed to the <ObjectType1>.Create(ParamFileName1, ...) >procedures for each of the objects in my model. This works fine. I would >also like to have a set of default parameters compiled into the code. Hard- >coding this would be simple, but I want instead to include as the default >a specified file from my source tree, verbatim, then use the same parsing >routines to process either the file image embedded in the code or one read >under a file name, as transparently as possible using approaches similar to >the earlier suggestions in this thread: String'Value( ParamStr ) ... etc. I'm not sure exactly what you're trying to do, but here's a suggestion. Can't you use a function call that returns a string as your default value? ie function Default_Value return String; procedure P (O : T := Default_Value); and then the Default_Value function could parse a file, read an environment variable, etc. The function gets invoked at run-time, and only then if the call of P doesn't have an explicit value as an argument. Of course, if it's the same value every time it's invoked (it only changes across program executions), then you can calculate that value once at initialization, either via an explicit package initialization subprogram or implicitly via package elaboration (be careful of the latter). Is this in the ballpark of what you wish to do? -------------------------------------------------------------------- Matthew Heaney Software Development Consultant <mailto:matthew_heaney@acm.org> (818) 985-1271 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: String to Integer 1997-07-15 0:00 String to Integer Charles Hill ` (2 preceding siblings ...) 1997-07-16 0:00 ` Robert Dewar @ 1997-07-16 0:00 ` Martin C. Carlisle 1997-07-17 0:00 ` Michael F Brenner 4 siblings, 0 replies; 10+ messages in thread From: Martin C. Carlisle @ 1997-07-16 0:00 UTC (permalink / raw) In article <5qglt7$cba$1@news.fsu.edu>, Charles Hill <hill@cs.fsu.edu> wrote: >I have a string in which an integer is stored. I'd like to hear >from someone who might know a convenient way to convert this to >an actual integer. I'm wondering if there is a function built >into one of the Ada packages to do this, but I haven't found one >yet. Usually you can find any similar thing you might need in Ada.Text_IO In your case (e.g.): Ada.Integer_Text_IO.Get(From => My_String, Item => Integer_Value, Last => Last_Location_Read_From_String); There is also a shorter method using attributes: Integer_Value := Integer'Value(My_String); --Martin -- Martin C. Carlisle, Computer Science, US Air Force Academy mcc@cs.usafa.af.mil, http://www.usafa.af.mil/dfcs/bios/carlisle.html DISCLAIMER: This content in no way reflects the opinions, standard or policy of the US Air Force Academy or the United States Government. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: String to Integer 1997-07-15 0:00 String to Integer Charles Hill ` (3 preceding siblings ...) 1997-07-16 0:00 ` String to Integer Martin C. Carlisle @ 1997-07-17 0:00 ` Michael F Brenner [not found] ` <5qlrue$3ft@top.mitre.org> 4 siblings, 1 reply; 10+ messages in thread From: Michael F Brenner @ 1997-07-17 0:00 UTC (permalink / raw) To convert strings to integers manually, examine the following topics: finite state machines, loops, arrays, preconditions, +, *, and assignment. To convert automatically, examine the topics: image, value, pos, and unchecked conversion, and text_io. ^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <5qlrue$3ft@top.mitre.org>]
* Re: String to Integer [not found] ` <5qlrue$3ft@top.mitre.org> @ 1997-07-18 0:00 ` Samuel Mize 0 siblings, 0 replies; 10+ messages in thread From: Samuel Mize @ 1997-07-18 0:00 UTC (permalink / raw) Michael F Brenner wrote: > > The best response to this was the list of topics to look up, given by > Mike Brenner, not the actual solutions given by the others. Actual > solutions should be available on the PAL, however, and should be > referred to. > > Mike Brenner Gee, I wish I had the gall to denigrate the work of other people and laud myself in the third person. Define "best." Do you really think a topic keyword is more helpful than a brief description? Or do you consider a less helpful reply "better" somehow? It's not like we were asked for a whole homework assignment, just a pointer to a language feature. Any code in the PAL converting between strings and integers would be a pretty suspect example to learn from, since it's manually duplicating a built-in language feature (83 and 95). By all means, though, keep directing new programmers to learn about Ada by dredging through the megs of code at the PAL, if you consider that the best way to communicate basic concepts. Sam Mize ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~1997-07-19 0:00 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 1997-07-15 0:00 String to Integer Charles Hill 1997-07-15 0:00 ` John Herro 1997-07-16 0:00 ` Samuel Mize 1997-07-16 0:00 ` Robert Dewar 1997-07-17 0:00 ` Embedding Text Data in Source (Was Re: String to Integer) John M. Mills 1997-07-17 0:00 ` Samuel T. Harris 1997-07-19 0:00 ` Matthew Heaney 1997-07-16 0:00 ` String to Integer Martin C. Carlisle 1997-07-17 0:00 ` Michael F Brenner [not found] ` <5qlrue$3ft@top.mitre.org> 1997-07-18 0:00 ` Samuel Mize
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox