28 maja 2014

my_itoa


char* my_itoa(long long input)
{
//sprintf undefined in Visual Studio 2013 and older
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
	unsigned long long len = 1;
	long long bufInput = (input < 0) ? -input : input;

	//additional minus sign
	if (input < 0)
		++len;

	while (bufInput)
	{
		++len;
		bufInput /= 10;
	}

	char *bufStr = calloc(sizeof(char), ++len); //len+1 for ending zero
	snprintf(bufStr, len, "%lld", input);
	return bufStr;
}

TrackBack

TrackBack URL dla tej wiadomości:
https://blog.kkthx.pl/2014/05/my_itoa/trackback/

Napisz komentarz