int如何转换成wstring?

wangsiyuanoo 2008-10-22 10:54:36

int nK = dwSize/1024;
wstring strSize = itoa(nK)+ L"KB";

我得到一个文件的大小,即nK等于18,我想在输出的时候在后面加“KB”,为什么这样不行?
有别的办法么?
其实就是整数转换成字符串,boost转换就不要说了,我没有库。
关键说一下itoa()。
这里面有三个参数,三个参数应该怎么写呢,就我这个问题来说!
谢谢!
...全文
2528 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
egi003 2009-08-13
  • 打赏
  • 举报
回复
看看............
编程-鸟人-_-- 2008-10-22
  • 打赏
  • 举报
回复
你可以试试一下 sprintf()函数

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
int a=4237497;
int nK = a/1024;
char str[10];
memset(str,0,10);
sprintf(str,"%d KB",nK);
cout<<str<<endl;
return 0;
}
可以解决你说的问题!!!
wangsiyuanoo 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 anglecloudy 的回复:]
C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
int a=4237497;
int nK = a/1024;
char str[10];
memset(str,0,10);
itoa(nK,str,10);//最后一个10是十进制
string strSize=str; //我用的是wstring就会出现问题!!!
strSize+="KB";
cout<<strSize<<endl;
return 0;
}

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'char [10]' (or there is no acceptable conversion)
[/Quote]
test4ever 2008-10-22
  • 打赏
  • 举报
回复
我还以为可以用_itow原来不行

itoa(int,char*,int(进制2–36))
OenAuth.Core 2008-10-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

int main(void)
{
int a=4237497;
int nK = a/1024;
char str[10];
memset(str,0,10);
itoa(nK,str,10);//最后一个10是十进制
string strSize=str;
strSize+="KB";
cout<<strSize<<endl;
return 0;
}

Jinhao 2008-10-22
  • 打赏
  • 举报
回复
std::wstringstream ss;
ss<<dwSize/1024<<L" KB";
std::wstring strSize = ss.str();
Jinhao 2008-10-22
  • 打赏
  • 举报
回复
加个 #include <sstream> 不就搞定了?
wangsiyuanoo 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Chiyer 的回复:]
1楼的不是挺好的吗
[/Quote]
一楼的我会出现这个问题:
error C2079: 'ss' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
星羽 2008-10-22
  • 打赏
  • 举报
回复
1楼的不是挺好的吗
wangsiyuanoo 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chenhuaichao314 的回复:]
你可以试试一下 sprintf()函数

C/C++ code
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
int a=4237497;
int nK = a/1024;
char str[10];
memset(str,0,10);
sprintf(str,"%d KB",nK);
cout < <str < <endl;
return 0;
}
可以解决你说的问题!!!
[/Quote]

我是想要wstring strSize 输出的是18KB,可你的str 怎么再转换成我的strSize 呢?
wince上各种类型转换特别是字符串是很头疼的,这个类是项目用的, 每个都测试好用,使用静态方法,直接引用后,写【::cv::就可以用】 typedef int u32; //转换类,所有的转换函数在这里静态 2是to的意思 //cs:cstring //s:string //i:int class cv { public: static char * s2charx(string s); //static char * cs2charx_unicode(CString s); static char * cs2charx_unicode(CString s); static CString charx2cs(char *s); cv(); virtual ~cv(); static int cstring_to_int(CString t); static CString s2cs(string text); static CString i2cs(int i); static string cs2s(CString s); static void Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static void Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static CString string_to_cstring(string text); static int string_to_int(string str); static string cstring_to_string(CString text); static void Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static BOOL IsNumeric(string text); static void UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen); static void Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen); static int cs2i(CString s); static string int_to_string(int n); static CString ChineseCapitalMoney(double Num); //static wstring s2ws( string s ); static std::wstring s2ws(const string& s); static std::string WChar2Ansi(LPCWSTR pwszSrc); static string ws2s(wstring& inputws); static const char * s2constcharx(string s); static std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen); static char * cs2charx_ansi(CString s); //static string ws2s(wstring ws); };

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧