⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faq41.htm

📁 C++builder学习资料C++builder
💻 HTM
📖 第 1 页 / 共 2 页
字号:


<HTML>

<HEAD>

   <TITLE>Converting AnsiStrings to numbers and converting numbers to AnsiStrings</TITLE>

   <META NAME="Author" CONTENT="Harold Howe">

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

<TABLE  BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">



<TR>

<TD>



<H3>

Converting <TT>AnsiStrings</TT> to numbers and converting numbers to <TT>AnsiStrings</TT>

</H3>

<BR>

<H3>Summary of tasks</H3>

<P>

This FAQ covers the following tasks:

<UL>

<LI> <A HREF="#strtoint">Converting AnsiStrings to integers</A>

<LI> <A HREF="#strtofloat">Converting AnsiStrings to floats or doubles</A>

<LI> <A HREF="#inttostr">Converting integers to AnsiStrings</A>

<LI> <A HREF="#floattostr">Converting floats or doubles to AnsiStrings</A>

</UL>

<H3><A NAME="strtoint">

Task 1: Converting <TT>AnsiStrings</TT> to integers:

</A></H3>

<P>

The VCL provides four functions to convert <TT>AnsiStrings</TT> to integers: the global

<TT>StrToInt</TT> and <TT>StrToIntDef</TT> functions, and the <TT>ToInt</TT> and

<TT>ToIntDef</TT> member functions of <TT>AnsiString</TT>. The <TT>StrToIntDef</TT>

and <TT>ToIntDef</TT> functions allow you to specify a default integer to return if the

string doesn't contain an valid integer. In addition to the VCL functions, you can still

use streams or the C run time library to convert an <TT>AnsiString</TT> to an <TT>int</TT>. Each

strategy is outlined below.

</P>

<B><TT>StrToInt</TT></B>

<P>

<TT>StrToInt</TT> is a global VCL function. The code for <TT>StrToInt</TT> resides in

<TT>SOURCE\SYSUTILS.PAS</TT>. The prototype for <TT>StrToInt</TT> is located in

<TT>INCLUDE\SYSUTILS.HPP</TT>. This header file is automatically included into your project

when <TT>VCL.H</TT> is included. <TT>StrToInt</TT> is easy to use: you simply pass it an

<TT>AnsiString</TT> and it returns an integer.

</P>



<pre>

<font color="navy">// this code converts a string</font>

<font color="navy">// into an integer number</font>

AnsiString str<b>(</b><font color="blue">"29"</font><b>)</b><b>;</b>

<b>int</b> nValue<b>;</b>

nValue <b>=</b> StrToInt<b>(</b>str<b>)</b><b>;</b>

</pre>



<TT>StrToInt</TT> generates an exception if the string does not contain a valid integer.

The exception is of the type <TT>EConvertError</TT>. This code demonstrates how you can

catch the exception.



<pre>

AnsiString str<b>=</b><font color="blue">"Get lost Buffy!"</font><b>;</b>

<b>int</b> nValue<b>;</b>

<b>try</b>

<b>{</b>

    nValue <b>=</b> StrToInt<b>(</b>str<b>)</b><b>;</b>

<b>}</b>

<b>catch</b> <b>(</b>EConvertError <b>&</b>e<b>)</b>

<b>{</b>

    Application<b>-></b>MessageBox<b>(</b><font color="blue">"It wasn't a number."</font> <b>,</b> <font color="blue">"error"</font><b>,</b> MB_OK<b>)</b><b>;</b>

    <b>return</b><b>;</b>

<b>}</b>

</pre>

<B><TT>AnsiString::ToInt</TT></B>

<P>

The <TT>Ansistring</TT> class contains a <TT>ToInt</TT> member function that converts the

string into an integer. The declaration for <TT>ToInt</TT> looks like this

(<TT>DSTRING.H</TT>):

</P>

<pre>

<b>int</b> <b>__fastcall</b> ToInt<b>(</b><b>)</b> <b>const</b><b>;</b>

</pre>

<P>

Notice that <TT>ToInt</TT> is declared as a const member function, which allows you to

call <TT>ToInt</TT> on constant strings. The code for <TT>ToInt</TT> is located in

<TT>SOURCE\VCL\DSTRING.CPP</TT>. If you have the VCL source, open up <TT>DSTRING.CPP</TT>.

You should see that <TT>ToInt</TT> simply calls the global <TT>StrToInt</TT> function. In

fact, all of the <TT>AnsiString</TT> conversion methods call their global counterparts.

Since <TT>ToInt</TT> simply calls <TT>StrToInt</TT>, you can surround <TT>ToInt</TT> with a

try-catch block to catch any conversion exceptions. Here is an example of how to use the

<TT>ToInt</TT> function.

</P>



<pre>

AnsiString str<b>(</b><font color="blue">"29"</font><b>)</b><b>;</b>

<b>int</b> nValue<b>;</b>

nValue <b>=</b> str<b>.</b>ToInt<b>(</b><b>)</b><b>;</b>

</pre>



<B><TT>StrToIntDef</TT> and <TT>AnsiString::ToIntDef</TT></B>

<P>

<TT>StrToIntDef</TT> and <TT>AnsiString::ToIntDef</TT> allow you to specify a default value

that will be returned if the <TT>AnsiString</TT> doesn't contain a valid integer. If the

string is not an integer, these functions return the default value instead of generating an

exception. If you have the VCL source, <TT>DSTRING.CPP</TT> reveals that the <TT>ToIntDef</TT>

method of <TT>AnsiString</TT> simply calls the global <TT>StrToIntDef</TT> function.

Here are code examples:

</P>

<pre>

<font color="navy">// StrToIntDef</font>

<font color="navy">// this code converts a string from an edit box</font>

<font color="navy">// into an integer number. Default value is 29.</font>

<b>int</b> nValue<b>;</b>

nValue <b>=</b> StrToIntDef <b>(</b>Edit1<b>-></b>Text<b>,</b> <font color="blue">29</font><b>)</b><b>;</b>



<font color="navy">// AnsiString::ToIntDef</font>

<font color="navy">// this code converts a string from an edit box</font>

<font color="navy">// into an integer number. Default value is 29.</font>

<b>int</b> nValue<b>;</b>

nValue <b>=</b> Edit1<b>-></b>Text<b>.</b>ToIntDef<b>(</b><font color="blue">29</font><b>)</b><b>;</b>

</pre>



<B>Using the C RTL or iostreams to convert an <TT>AnsiString</TT> to an integer</B>

<P>

The VCL <TT>StrToInt</TT> function requires that the entire string be a valid number. If

you call <TT>StrToInt</TT> for an <TT>AnsiString</TT> that contains "1100 Grand Avenue",

you might expect that the return value will be 1100. Not so. Instead, <TT>StrToInt</TT>

sees that the string contains non-numeric characters and generates an exception. For strings

like these, you can use either the C RTL <TT>sscanf</TT> function, or use an iostream. This

technique requires that you call the <TT>c_str</TT> member function to retrieve a <TT>char *</TT>

that you can pass to the RTL.

</P>



<pre>

<font color="navy">// using sscanf to read numbers from an AnsiString</font>

<font color="navy">// you need to add the standard sscanf error handling code</font>

<font color="navy">// don't forget to #include &lt;stdio.h></font>

<b>int</b> i<b>,</b>j<b>;</b>

AnsiString strEdit <b>=</b> <font color="blue">"29 100"</font><b>;</b>

sscanf<b>(</b>strEdit<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <font color="blue">"%d %d"</font><b>,</b><b>&</b>i<b>,</b> <b>&</b>j<b>)</b><b>;</b>

ProgressBar1<b>-></b>Position <b>=</b> i<b>;</b>

ProgressBar2<b>-></b>Position <b>=</b> j<b>;</b>



<font color="navy">// using streams to read numbers from an AnsiString</font>

<font color="green">#include &lt;sstream></font>

<b>using</b> <b>namespace</b> std<b>;</b>

<b>...</b>

<b>int</b> i<b>,</b>j<b>;</b>

AnsiString strEdit <b>=</b> <font color="blue">"29 100"</font><b>;</b>

istrstream istr<b>(</b>strEdit<b>.</b>c_str<b>(</b><b>)</b><b>,</b> strEdit<b>.</b>Length<b>(</b><b>)</b><b>)</b><b>;</b>

istr <b>>></b> i <b>>></b> j<b>;</b>

ProgressBar1<b>-></b>Position <b>=</b> i<b>;</b>

ProgressBar2<b>-></b>Position <b>=</b> j<b>;</b>

</pre>



<H3><A NAME="strtofloat">

Task 2: Converting <TT>AnsiStrings</TT> to floating point numbers:

</A></H3>

<P>

The VCL provides two functions that convert <TT>AnsiStrings</TT> to floating point

numbers: the global <TT>StrToFloat</TT> function and the <TT>ToDouble</TT> member

function of <TT>AnsiString</TT> (<TT>ToDouble</TT> simply calls <TT>StrToFloat</TT>).

You can also use streams or the C RTL (see previous code about converting strings to ints).

The following example demonstrates how to use <TT>StrToFloat</TT> and

<TT>AnsiString::ToDouble</TT>.

</P>

<pre>

<font color="navy">// using StrToFloat to convert an AnsiString to a float</font>

AnsiString str<b>(</b><font color="blue">"3.14159"</font><b>)</b><b>;</b>

<b>float</b> fValue<b>;</b>

fValue <b>=</b> StrToFloat<b>(</b>str<b>)</b><b>;</b>



<font color="navy">// using AnsiString::ToDouble to convert an AnsiString to a float</font>

AnsiString str<b>(</b><font color="blue">"3.14159"</font><b>)</b><b>;</b>

<b>float</b> fValue<b>;</b>

fValue <b>=</b> str<b>.</b>ToDouble<b>(</b><b>)</b><b>;</b>

</pre>

<P>

<TT>StrToFloat</TT> returns an 80 bit <TT>long double</TT>, whereas <TT>ToDouble</TT>

returns a 64 bit double. Both of the preceeding code examples could have used the

<TT>double</TT> data type instead of <TT>float</TT>. <TT>StrToFloat</TT>

generates an exception if the <TT>AnsiString</TT> contains invalid text.

<TT>StrToFloat</TT> works by calling the <TT>TextToFloat</TT> pascal function

in <TT>SYSUTILS.PAS</TT>.

</P>

<P>

The section on how to convert an <TT>AnsiString</TT> to an integer contained

a section that described how to use <TT>sscanf</TT> or <TT>istringstream</TT>

to extract integer values from a string. These examples could also be extended

to floating point types.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -