Skip to main content
Engineering LibreTexts

19: Character String / Numeric Conversion

  • Page ID
    54228
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Characters string values, such as “123” cannot be used to perform numeric operations such as addition or multiplication. As such, for more complex programs, there is sometimes the need to convert between a character string representing a numeric value and an actual real or integer number.

    These conversions can be performed using what is referred to as an internal read or an internal write. Basically, the read or write functions and associated format statements can be used to perform basic conversions. Instead of reading from an open file, the read and write operations can read and write directly from and to variables. The specified format provides guidance for the conversion result.

    Based on the input, a conversion may not be possible. For example, the character string “3.14” can be converted into the real value of 3.14. However, the character string “3.1z4” could not be converted since the 'z' is not a legal numeric value.

    If a conversion is not possible, an error would be generated. If not handled, such an error would crash the program. In order to address and handle any potential errors, the iostat parameter for the read/write operation is used as previously described in the file operations chapter.


    This page titled 19: Character String / Numeric Conversion is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?