Board index » delphi » itoa (Your oppinion)

itoa (Your oppinion)

I am converting an integer to an asci. What size should I make my
buffer to be and why. I need this code to last through the upcoming years with no maintenance, and to fly through my code
review.
 int Question = ????
 char buffer[Question];

  SynId = CString(itoa(lowerVal,buffer,10));

 

Re:itoa (Your oppinion)


How many digits are possible in an integer? 10. So, 10 + 1 for the
terminating null.

--
Warren Zeigler Sr.
wzeig...@UnderstandingObjects.com
Wrap your mind around objects quickly.

Quote
"Brenda" <binsoft@msn> wrote in message news:3cc87b56$1_2@dnews...

> I am converting an integer to an asci. What size should I make my
> buffer to be and why. I need this code to last through the upcoming years

with no maintenance, and to fly through my code
Quote
> review.
>  int Question = ????
>  char buffer[Question];

>   SynId = CString(itoa(lowerVal,buffer,10));

Re:itoa (Your oppinion)


On Thu, 25 Apr 2002 17:04:47 -0600, "Warren R. Zeigler"

Quote
<wzeig...@UnderstandingObjects.com> wrote:
>How many digits are possible in an integer? 10. So, 10 + 1 for the
>terminating null.

... and if her program is compiled under 64-bit Windows, will this suffice?

Recall that she said:

Quote
>>I need this code to last through the upcoming years with no maintenance

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:itoa (Your oppinion)


wak...@idirect.com (Wayne A. King) wrote:

Quote
>On Thu, 25 Apr 2002 17:04:47 -0600, "Warren R. Zeigler"
><wzeig...@UnderstandingObjects.com> wrote:

>>How many digits are possible in an integer? 10. So, 10 + 1 for the
>>terminating null.

>... and if her program is compiled under 64-bit Windows, will this suffice?

And for 128 bit Windows? <G>

So say, 20 digits plus null, plus minus(don't forget the sign!) for a
total of 22.  More if you want to add (6) commas.

Re:itoa (Your oppinion)


Quote
"Brenda" <binsoft@msn> writes:
> I am converting an integer to an asci. What size should I make my
> buffer to be and why.

Assuming you use a decimal string representation (I don't know the itoa
function), the buffer needs enough space for
std::numeric_limits<int>::digits10 decimal digits, the sign and the
terminating '\0'.

char buffer[std::numeric_limits<int>::digits10+2];
using std::sprintf;
if (sprintf(buffer,"%d",lowerVal)==1)
{
  // use buffer

Quote
}

As a generic solution (for integral types), you could use the expression

std::numeric_limits<T>::digits10 + std::numeric_limits<T>::is_signed + 1

Other Threads

1. itoa