Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Efficient itos();

7 views
Skip to first unread message

KitKat

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
Hi,

I believe this question pops up regurlarly but I guess I just never
saw the real end of it (some code). I would just like to have a piece of
code that efficiently translates integers to strings. Just so that I don't
look like a beggar, I've done my homework and forged myself one. Besides,
how come is this function not in the standard since atoi() is (and hence
stoi())?

#include <string>
#include <cstdlib>

const short ascii_offset_num = 48;

string itos(int i){
string s = "";
bool negative = (i != abs(i));
i = abs(i);
while(i > 0){
s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;
i /= 10;
}
return s.empty()? "0" : negative? "-" + s : s;
}


Mike Wahler

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to

KitKat <kitk...@videotron.ca> wrote in message
news:m_4b5.2838$A7.9...@wagner.videotron.net...


#define STRING_LEN 20
#include <stdio.h>
int main()
{
int i = 42;
char the_string[STRING_LEN];
sprintf(the_string, "%d", i);
printf("the_string = %s\n", the_string);
return 0;
}

-Mike

Victor Bazarov

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to
"KitKat" <kitk...@videotron.ca> wrote...

> Hi,
>
> I believe this question pops up regurlarly but I guess I just
never
> saw the real end of it (some code). I would just like to have a piece
of
> code that efficiently translates integers to strings. Just so that I
don't
> look like a beggar, I've done my homework and forged myself one.
Besides,
> how come is this function not in the standard since atoi() is (and
hence
> stoi())?

Because there is no C Standard type 'string', no?

>
> #include <string>
> #include <cstdlib>

using std::string;

>
> const short ascii_offset_num = 48;

const short ascii_offset_num = '0';

>
> string itos(int i){
> string s = "";

just
string s;
will suffice

> bool negative = (i != abs(i));
> i = abs(i);
> while(i > 0){
> s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;

Why 'div(i, 10).rem', can't you just say 'i % 10'?

> i /= 10;
> }
> return s.empty()? "0" : negative? "-" + s : s;
> }

On some systems there is a negative zero. Have you accounted for that?

Victor
--
Please remove capital A's from my address when replying by mail


KitKat

unread,
Jul 12, 2000, 3:00:00 AM7/12/00
to

Victor Bazarov <vAba...@dAnai.com> a écrit dans le message :
8kipds$3p9$1...@bob.news.rcn.net...
> "KitKat" <kitk...@videotron.ca> wrote...

> Because there is no C Standard type 'string', no?

1. But there is one is Standard C++.
2. So what? How about an itoa() then?


> > const short ascii_offset_num = 48;
> const short ascii_offset_num = '0';

What is the difference? (Apart from the fact that you are implicitly
casting.)


> > s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;
> Why 'div(i, 10).rem', can't you just say 'i % 10'?

Oops. Got stupid on that one...


> On some systems there is a negative zero. Have you accounted for that?

I'd be curious to see the bit pattern for that one... I suppose I
assumed your good ol' two's complement architecture. No big harm in that
assumption I believe.

Michael Migal

unread,
Jul 13, 2000, 3:00:00 AM7/13/00
to
How about just simply use any of the _itoa(), _i64toa(), _ui64toa(),
_itow(), _i64tow(), _ui64tow() from the <stdlib.h>?

Mike

"KitKat" <kitk...@videotron.ca> wrote in message
news:m_4b5.2838$A7.9...@wagner.videotron.net...

> Hi,
>
> I believe this question pops up regurlarly but I guess I just
never
> saw the real end of it (some code). I would just like to have a piece of
> code that efficiently translates integers to strings. Just so that I don't
> look like a beggar, I've done my homework and forged myself one. Besides,
> how come is this function not in the standard since atoi() is (and hence
> stoi())?
>

> #include <string>
> #include <cstdlib>


>
> const short ascii_offset_num = 48;
>

> string itos(int i){
> string s = "";

> bool negative = (i != abs(i));
> i = abs(i);
> while(i > 0){

> s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;

Dietmar Kuehl

unread,
Jul 13, 2000, 3:00:00 AM7/13/00
to kitk...@videotron.ca
Hi,
In article <m_4b5.2838$A7.9...@wagner.videotron.net>,

"KitKat" <kitk...@videotron.ca> wrote:
> I believe this question pops up regurlarly but I guess I just never
> saw the real end of it (some code).

Suitable code was posted often enough:

#include <sstream>
std::string itos(int i) {
std::ostringstream out;
out << i;
return out.str();
}

If this is not suitable, just dig further in the news and you will find
code like eg. this:

std::string itos(int i) {
typedef std::num_put<char, std::back_insert_iterator<char> > NP;
std::locale loc;
if (!std::has_facet<NP>(loc))
loc = std::locale(new NP, loc);
std::string rc;
std::use_facet<NP>.put(std::back_inserter(rc), std::cout, ' ', i);
return rc;
}

On <http://www.boost.org/> a "stream_cast" was discussed and I think it
should be available in one of the files: Basically this cast just
converts from one type to another using string streams, ie. it is
implemented like this:

template <class S, class T>
T stream_cast(S const& s) {
std::stringstream stream;
stream << s;
T t;
stream >> t;
return t;
}

This way, you can convert an integer to a string like this:

std::string str = stream_cast<std::string>(1234);

... and this does not only work with integer to string but also with
arbitrary combinations as long as the corresponding inserter and
extractors are defined.
--
<mailto:dietma...@yahoo.com>
<http://www.dietmar-kuehl.de/>


Sent via Deja.com http://www.deja.com/
Before you buy.

Mike Wahler

unread,
Jul 13, 2000, 3:00:00 AM7/13/00
to
KitKat <kitk...@videotron.ca> wrote in message
news:8Rab5.5403$V12.1...@weber.videotron.net...

>
> Victor Bazarov <vAba...@dAnai.com> a écrit dans le message :
> 8kipds$3p9$1...@bob.news.rcn.net...
> > "KitKat" <kitk...@videotron.ca> wrote...
> > Because there is no C Standard type 'string', no?
>
> 1. But there is one is Standard C++.
> 2. So what? How about an itoa() then?

See std::ostringstream for a C++ way to do what
I did with sprintf() in my other post. (At first I didn't
notice you were using C++ and not C.

#include <sstream>
#include <string>
#include <iostream>

int main()
{
int value = 123; // sample int

std::ostringstream oss; // string stream
oss << value; // insert the int

std::string s(oss.str()); // create string from s-stream
std::cout << s << std::endl; // display string

return 0;
}

>
>
> > > const short ascii_offset_num = 48;

> > const short ascii_offset_num = '0';
>
> What is the difference? (Apart from the fact that you are implicitly
> casting.)

The difference is that one way (Victor's) is portable, the other
(yours) is not. The value 48 represents the numeral '0' in ASCII.
Not all machines use the ASCII character set. Use the literal '0',
and the proper value for a given platform's '0' character will be used.

-Mike

>
>
> > > s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;

Mike Wahler

unread,
Jul 13, 2000, 3:00:00 AM7/13/00
to

Michael Migal <mi...@emedius.com> wrote in message
news:Uteb5.61919$_b3.1...@newsread1.prod.itd.earthlink.net...

> How about just simply use any of the _itoa(), _i64toa(), _ui64toa(),
> _itow(), _i64tow(), _ui64tow() from the <stdlib.h>?

Probably becuase those are not standard functions, and
as such are not available in all implementations.

-Mike

>
> Mike


>
> "KitKat" <kitk...@videotron.ca> wrote in message

> news:m_4b5.2838$A7.9...@wagner.videotron.net...
> > Hi,


> >
> > I believe this question pops up regurlarly but I guess I just
> never

> > saw the real end of it (some code). I would just like to have a piece of
> > code that efficiently translates integers to strings. Just so that I
don't
> > look like a beggar, I've done my homework and forged myself one.
Besides,
> > how come is this function not in the standard since atoi() is (and hence
> > stoi())?
> >
> > #include <string>
> > #include <cstdlib>
> >

> > const short ascii_offset_num = 48;
> >

> > string itos(int i){
> > string s = "";
> > bool negative = (i != abs(i));
> > i = abs(i);
> > while(i > 0){

> > s = static_cast<char>(div(i, 10).rem + ascii_offset_num) + s;

0 new messages