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

📄 faq41.htm

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

<H3><A NAME="inttostr">

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

</A></H3>

<P>

The VCL provides two functions to convert an integer to an <TT>AnsiString</TT>: the global

<TT>IntToStr</TT> function and a version of the <TT>AnsiString</TT> constructor. Once again,

nothing prevents you from using iostreams or the C run time library to do the conversion.

The examples below demonstrate each method.

</P>

<B><TT>IntToStr</TT></B>

<P>

The prototype for <TT>IntToStr</TT> is located in <TT>SYSUTILS.HPP</TT>, which is

automatically included by <TT>VCL.H</TT>. The code for <TT>IntToStr</TT> is located in

<TT>SYSUTILS.PAS</TT>. The function would look like this if it were written in C++:

</P>

<pre>

AnsiString <b>__fastcall</b> IntToStr<b>(</b><b>int</b> Value<b>)</b>

<b>{</b>

    AnsiString RetValue<b>;</b>

    FmtStr<b>(</b>RetValue<b>,</b> <font color="blue">"%d"</font><b>,</b> Value<b>)</b><b>;</b>

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

<b>}</b>

</pre>

<TT>FmtStr</TT> is a pascal function that works a lot like the C <TT>sprintf</TT> function.

The code example below shows how to use <TT>IntToStr</TT>.

<pre>

<b>int</b> nValue     <b>=</b> TrackBar1<b>-></b>Position<b>;</b>

AnsiString str <b>=</b> IntToStr<b>(</b>nValue<b>)</b><b>;</b>

Edit1<b>-></b>Text    <b>=</b> str<b>;</b>

</pre>

<B><TT>AnsiString::AnsiString(int Value)</TT></B>

<P>

<TT>AnsiString</TT> contains a constructor that can initialize the string with an integer

value. Here's how you do it:

</P>

<pre>

<b>int</b> nValue <b>=</b> TrackBar1<b>-></b>Position<b>;</b>

AnsiString str<b>(</b>nValue<b>)</b><b>;</b>

Edit1<b>-></b>Text <b>=</b> str<b>;</b>

</pre>

<B>Using the <TT>sprintf</TT> member function of <TT>AnsiString</TT></B>

<P>

In C++Builder 4, Borland added 2 member functions to AnsiString called <TT>sprintf</TT>

and <TT>printf</TT>. These functions work much like the standalone <TT>sprintf</TT>

function in the RTL. The difference is that <TT>AnsiString::sprintf</TT> formats

the <TT>AnsiString</TT> object, instead of formatting a <TT>char *</TT>.

</P>

The code example below shows how you can use the <TT>sprintf</TT> member function

of <TT>AnsiString</TT>.

</P>



<pre>

<font color="navy">// using sprintf to read numbers into an AnsiString</font>

<b>int</b> i <b>=</b> <font color="blue">29</font><b>;</b>

<b>int</b> j <b>=</b> <font color="blue">5006</font><b>;</b>

AnsiString str<b>;</b>

str<b>.</b>sprintf<b>(</b><font color="blue">"The numbers are %d and %d."</font><b>,</b> i<b>,</b>j<b>)</b><b>;</b>

Label1<b>-></b>Caption <b>=</b> str<b>;</b>

</pre>



<P>

This is a very useful function. Notice that you don't have to pass a destination

buffer to <TT>AnsiString::sprintf</TT>. This is because the AnsiString itself

is the destination.

</P>

<P>

<TT>AnsiString</TT> contains several other member functions that behave like

the <TT>sprintf</TT> function. They are <TT>printf</TT>, <TT>vprintf</TT>,

<TT>cat_sprintf</TT>, <TT>cat_printf</TT>, and <TT>cat_vprintf</TT> (the last

four may only be available in BCB5 and beyond, check <TT>dstring.h</TT> to be sure).

The <TT>printf</TT> member funtion works just like <TT>sprintf</TT>, except that

it returns the length of the string after the formatting has been done. <TT>sprintf</TT>

does not return anything. Don't confuse <TT>AnsiString::printf</TT> with the regular

<TT>printf</TT> function. The RTL <TT>printf</TT> routine prints to <TT>stdout</TT>.

<TT>AnsiString::printf</TT> writes to the <TT>AnsiString</TT> object.

</P>

<P>

The <TT>vprintf</TT> member of <TT>AnsiString</TT> behaves sort of like the

RTL <TT>vprintf</TT> function. Unlike <TT>sprintf</TT> and <TT>printf</TT>, this

function takes a <TT>va_list</TT> argument, instead of a variable number of

arguments. The <TT>printf</TT> and <TT>sprintf</TT> routines both rely on the

<TT>vprintf</TT> function for their underlying implementation.

</P>

<P>

All of the cat routines concatenate the formatted string onto whatever was already

in the <TT>AnsiString</TT>. Here is an example that demonstrates <TT>cat_sprintf</TT>

</P>



<pre>

<font color="navy">// using cat_sprintf to read numbers into an AnsiString</font>

<b>int</b> i <b>=</b> <font color="blue">29</font><b>;</b>

<b>int</b> j <b>=</b> <font color="blue">5006</font><b>;</b>

AnsiString str <b>(</b><font color="blue">"I was already here: "</font><b>)</b><b>;</b>

str<b>.</b>sprintf<b>(</b><font color="blue">"The numbers are %d and %d."</font><b>,</b> i<b>,</b>j<b>)</b><b>;</b>

Label1<b>-></b>Caption <b>=</b> str<b>;</b>

</pre>



<P>

After calling <TT>cat_sprintf</TT>, the string contains the text

"I was already here: The numbers are 29 and 5006."

</P>



<P>

<B>Using <TT>ostringstream</TT> to read numbers into an <TT>AnsiString</TT></B>

</P>

<P>

You can also use an <TT>ostringstream</TT> to format an <TT>AnsiString</TT> variable. In fact,

this method may be better than using the <TT>sprintf</TT> because it does not

rely on format codes and variable argument functions. Here is a code

example.

</P>

<pre>

<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> <font color="blue">29</font><b>;</b>

<b>int</b> j <b>=</b> <font color="blue">5006</font><b>;</b>

AnsiString str<b>;</b>

ostringstream ostr<b>;</b>

ostr <b><<</b> <font color="blue">"The numbers are "</font><b><<</b> i <b><<</b> <font color="blue">" and "</font> <b><<</b> j <b><<</b> <font color="blue">"."</font> <b>;</b>

str <b>=</b> ostr<b>.</b>str<b>(</b><b>)</b><b>.</b>c_str<b>(</b><b>)</b><b>;</b>

Label1<b>-></b>Caption <b>=</b> str<b>;</b>

</pre>



<H3><A NAME="floattostr">

Task 4: Converting floats to <TT>AnsiStrings</TT>:

</A></H3>

<P>

The VCL provides several functions that convert floating point numbers to

<TT>AnsiString</TT>s. You can use the <TT>FloatToStr</TT> or <TT>FloatToStrF</TT>

global functions, or you can use an alternative <TT>AnsiString</TT> constructor

that takes a <TT>double</TT> value as an argument. Once again, you can always

resort to iostreams or the C RTL (see the examples in how to convert an int to a string).

</P>

<B><TT>FloatToStr</TT> and <TT>FloatToStrF</TT></B>

<P>

Both <TT>FloatToStr</TT> and <TT>FloatToStrF</TT> work by calling a pascal function

called <TT>FloatToText</TT>. <TT>FloatToText</TT> takes arguments that allow you

to customize the format of the resulting string. It allows you to specify the

format, the precision, and a number that represents how many digits to display.

The format is chosen by specifying a value from the <TT>TFloatFormat</TT> enum.

<TT>FloatToStr</TT> hard codes the format to <TT>ffGeneral</TT>, the precision to 15,

and the digit value to 0. <TT>FloatToStrF</TT> allows you to specify these values,

which makes it the more useful function of the two. Here is a code example of both.

</P>

<pre>

<font color="navy">// an example of FloatToStr</font>

<b>float</b> fValue <b>=</b> <font color="blue">100.2</font><b>;</b>

AnsiString str <b>=</b> FloatToStr<b>(</b>fValue<b>)</b><b>;</b>



<font color="navy">// an example of FloatToStrF</font>

<b>float</b> fValue <b>=</b> <font color="blue">100.2</font><b>;</b>

AnsiString str <b>=</b> FloatToStrF<b>(</b>fValue<b>,</b>ffGeneral<b>,</b><font color="blue">7</font><b>,</b><font color="blue">5</font><b>)</b><b>;</b>



<font color="navy">// an example of FloatToStrF using scientific notation</font>

<b>float</b> fValue <b>=</b> <font color="blue">10</font>e<b>-</b><font color="blue">6</font><b>;</b>

AnsiString str <b>=</b> FloatToStrF<b>(</b>fValue<b>,</b>ffExponent<b>,</b> <font color="blue">7</font><b>,</b><font color="blue">5</font><b>)</b><b>;</b>

</pre>

<P>

The third argument to <TT>FloatToStrF</TT> determines the precision of the

conversion. This value should be 7 or less for single precision float types,

and should be 15 or less for double's. The last parameter is the digits argument.

This meaning of the digits argument varies for different conversion modes

(<TT>ffExponent</TT> verses <TT>ffGeneral</TT>). Consult the help file for

specific details.

</P>

<B><TT>AnsiString::AnsiString(double Value)</TT></B>

<P>

The <TT>AnsiString</TT> class provides a constructor that allows you to initialize the

string with a double precision floating point value. The constructor works by

calling <TT>FloatToStrF</TT>.

</P>



<pre>

<font color="navy">// an example of AnsiString::AnsiString(double src)</font>

<b>float</b> fValue <b>=</b> <font color="blue">100.2</font><b>;</b>

AnsiString str<b>(</b>vFalue<b>)</b><b>;</b>

Label1<b>-></b>Caption <b>=</b> str<b>;</b>

</pre>



</TD> </TR>



</TABLE>

</CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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