int64相互转换字符串

梦想游戏人
目录:
C/C++

itoa 不支持64位整数的转换,我们可以吧大整数拆为2段来包装itoa

string  Utils::ull_to_string(unsigned long long _num)
{
	const unsigned long long  _div = 10000000;

	unsigned long long    upper_part = _num / _div;
	unsigned long long  lower_part = _num - upper_part * _div;

	char buf[100];

	memset(buf, 0, sizeof (char)* 100);

	itoa((int)upper_part, buf, 10);
	string ret = buf;

	memset(buf, 0, sizeof (char)* 100);
	itoa((int)lower_part, buf, 10);
	ret.append(buf);


	while (ret[0] == '0')
	{
		ret = ret.substr(1, -1);

	}

	return ret;

}

int64转换为string

unsigned long long  Utils::string_to_ull(const string num)
{
	unsigned long long _file_size = 0;
	unsigned long long des = 1;
	for (int i = num.size() - 1;
		i >= 0; i--, des *= 10)
	{
		_file_size += des*(num[i] - '0');
	}
	return _file_size;
}
Scroll Up