如何在C ++中将long转换为字符串? [英] How do I convert a long to a string in C++?

查看:255
本文介绍了如何在C ++中将long转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中将 long 转换为字符串

How do I convert a long to a string in C++?

推荐答案

您可以使用stringstream。

You could use stringstream.

#include <sstream>

// ...
std::string number;
std::stringstream strstream;
strstream << 1L;
strstream >> number;

在编译器的标准库中通常有一些专有的C函数。我喜欢更多的便携变体虽然。

There is usually some proprietary C functions in the standard library for your compiler that does it too. I prefer the more "portable" variants though.

C的方法做到这一点是sprintf,但是不是很安全。在一些库中有新的版本,如sprintf_s,防止缓冲区溢出。

The C way to do it would be with sprintf, but that is not very secure. In some libraries there is new versions like sprintf_s which protects against buffer overruns.

这篇关于如何在C ++中将long转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆