2.3 NumericString Conversions


2.3 Numeric/String Conversions

Because most programming languages (or their libraries) provide automatic numeric/string conversions, beginning programmers are often unaware that this conversion is even taking place. In this section we'll consider two conversions: from string to numeric form and from numeric form to string.

Consider how easy it is to convert a string to numeric form in various languages. In each of the following statements, the variable i can hold some integer number. The input from the user 's console, however, is a string of characters. The programming language's run-time library is responsible for converting that string of characters to the internal binary form the CPU requires.

 cin >> i;     // C++  readln( i );  // Pascal  input i       // BASIC  stdin.get(i); // HLA 

Because these statements are so easy to use, most programmers don't consider the cost of using such statements in their programs. Unfortunately, if you have no idea of the cost of these statements, you'll not realize how they can impact your program when performance is critical. The reason for exploring these conversion algorithms here is to make you aware of the work involved so you will not make frivolous use of these conversions.

To simplify things, we'll discuss unsigned integer values and ignore the possibility of illegal characters and numeric overflow. Therefore, the following algorithms actually understate the actual work involved (by a small amount).

Use this algorithm to convert a string of decimal digits to an integer value:

  1. Initialize a variable with zero; this will hold the final value.

  2. If there are no more digits in the string, then the algorithm is complete, and the variable holds the numeric value.

  3. Fetch the next digit (going from left to right) from the string.

  4. Multiply the variable by ten, and then add in the digit fetched in step 3.

  5. Go to step 2 and repeat.

Converting an integer value to a string of characters takes even more effort. The algorithm for the conversion is the following:

  1. Initialize a string to the empty string.

  2. If the integer value is zero, then output a , and the algorithm is complete.

  3. Divide the current integer value by ten, computing the remainder and quotient .

  4. Convert the remainder (always in the range 0..9) to a character, and concatenate the character to the end of the string.

  5. If the quotient is not zero, make it the new value and repeat steps 3-5.

  6. Output the characters in the reverse order they were placed into the string.

The particulars of these algorithms are not important. What is important to note is that these steps execute once for each output character and division is very slow. So a simple statement like one of the following can hide a fair amount of work from the programmer:

 printf( "%d", i );  // C  cout << i;          // C++  print i             // BASIC  write( i );         // Pascal  stdout.put( i );    // HLA 

To write great code you don't need to eschew the use of numeric/string conversions. They are an important part of computation, and great code will need to do these conversions. However, a great programmer will be careful about the use of numeric/string conversions and only use them as necessary. Note that these algorithms are only valid for unsigned integers. Signed integers require a little more effort to process (though the extra work is almost negligible). Floating-point values, however, are far more difficult to convert between string and numeric form. That's something to keep in mind when writing code that uses floating-point arithmetic.




Write Great Code. Understanding the Machine, Vol. 1
The Art of Assembly Language
ISBN: 1593270038
EAN: 2147483647
Year: 2003
Pages: 144
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net