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

📄 faq45.htm

📁 C++builder学习资料C++builder
💻 HTM
字号:


<HTML>

<HEAD>

   <TITLE>Display an integer in HEX or binary format</TITLE>

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

</HEAD>

<BODY BGCOLOR="WHITE">



<CENTER>

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

<TR>

<TD>

<H3>

Display an integer in HEX or binary format

</H3>

<P>

The VCL contains a global function called <TT>IntToHex</TT> that takes an integer and

creates an <TT>AnsiString</TT> that contains the hex representation of the number. <TT>IntToHex</TT>

is located in <TT>SYSUTILS.PAS</TT>. It takes two arguments: the first is the number that you want to

display in HEX format, and the second argument determines how many digits you want to display.

Here is an example:

</P>

<pre>

<b>int</b> nValue <b>=</b> <font color="blue">378</font><b>;</b>

Label1<b>-></b>Caption <b>=</b> IntToHex<b>(</b>nValue<b>,</b><font color="blue">8</font><b>)</b><b>;</b>

</pre>

<P>

You can also use the <TT>itoa</TT> function from the C RTL. <TT>itoa</TT> allows

you to specify a radix argument. Use a radix of 16 to convert the number to HEX

format. <TT>itoa</TT> has the extra benefit that it can convert the number to

binary format.

</P>

<pre>

<b>int</b> nValue <b>=</b> <font color="blue">452</font><b>;</b>

<b>char</b> buf<b>[</b><font color="blue">40</font><b>]</b><b>;</b>

itoa<b>(</b>nValue<b>,</b> buf<b>,</b> <font color="blue">2</font><b>)</b><b>;</b>  <font color="navy">// 2 means binary, 16 would be hex</font>

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

</pre>

<P>

Of course, you could also use streams, the RTL <TT>sprintf</TT> function, or

the <TT>sprintf</TT> member function of <TT>AnsiString</TT> (new in BCB4).

</P>

<pre>

<font color="navy">// using streams to display a number in hex.</font>

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

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

<b>...</b>

<b>...</b>

<b>int</b> nValue <b>=</b> <font color="blue">452</font><b>;</b>

ostringstream ostr<b>;</b>

ostr <b><<</b> hex <b><<</b> nValue<b>;</b>

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



<font color="navy">// using sprintf to display a number in hex.</font>

<b>int</b> nValue <b>=</b> <font color="blue">452</font><b>;</b>

<b>char</b> buf<b>[</b><font color="blue">40</font><b>]</b><b>;</b>

sprintf<b>(</b>buf<b>,</b> <font color="blue">"%08X"</font><b>,</b> nValue<b>)</b><b>;</b>

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



<font color="navy">// using AnsiString::sprintf to display a number in hex.</font>

<b>int</b> nValue <b>=</b> <font color="blue">452</font><b>;</b>

AnsiString str<b>;</b>

str<b>.</b>sprintf<b>(</b><font color="blue">"%08X"</font><b>,</b> nValue<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 + -