如何转换_int64 到 string

本文介绍了几种在C++中将_int64类型的数据转换为string类型的有效方法,包括使用C++11标准库中的to_string函数、stringstream类以及传统的sprintf或_i64toa函数。

文章目录


参考 stackoverflow: converting _int64 to a string,验证建议的代码可以成功地将_int64转换到string。

C++ 11的标准做法:

#include <string>

int main()
{
  int64_t value = 100;
  std::string asString = std::to_string(value);
  return 0;
}

使用stringstream:

#include <string>
#include <sstream>

int main()
{
  std::stringstream stream;
  __int64 value(1000000000);
  stream << value;    
  std::string strValue(stream.str());
  return 0;
}

使用C sprintf() 或者 _i64toa()

__int64 value = ...;
char buffer[20];
sprintf(buffer, "%Ld", value);

__int64 value = ...;
char buffer[20];
_i64toa(value, buffer, 10);

参考

stackoverflow: converting _int64 to a string
Microsoft Docs: _itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值