itoa'd you so?

Steve smorrey at gmail.com
Wed Sep 19 14:42:25 MDT 2007


As many of you may be aware I have been actively job hunting these
past couple of weeks.
I've gone to many interviews now where I've been asked to do some
pretty peculiar things, such as solving logic puzzles, write random
function x etc.
But this one takes the cake, mostly because the interviewer told me he
had "seen better" but could not or would not elaborate further.

Here is the setup.

There is no standard way to convert an int into a char*, or at least
there is nothing in the standard library.

So I was asked how I would write an itoa function.
The goal was to target an embedded platform, and so clock cycles and
overhead really count.

The solution could be in either C or C++

I came up with 2 solutions which both amounted to roughly the same code.

std::string itoa(int in){
	std::stringstream out;
	out << in;
	return(out.str());
}

and

char* itoa(int in){
	std::stringstream out;
	out << in;
	return(out.rdbuf.c_str());
}

So my question is, what is wrong with this method (I haven't tested it
so there may be a minor syntax error, but that aside)?

Seems to me it's the most efficient way to do it in a typesafe manner,
but maybe I'm just missing something.
Can anyone see anything wrong or maybe just a better way to do an itoa?

Sincerely,
Steve



More information about the PLUG mailing list