将int转换为字符串并显示 [英] Converting of int to string and displaying

查看:51
本文介绍了将int转换为字符串并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用_ttoi函数将字符串变量转换为int之后,我需要将其转换为String才能显示该值.这是代码:

After converting a string variable into int using _ttoi function , I need to convert it to String to display the value. This is the code :

My function:  

int i;
char buffer[20];
CString str;

i= _ttoi(myVal->GetText());  //convert string to int 
itoa (i,buffer, 10); //convert int back to string 

str.AppendFormat(_T("Value is : %s\n"), buffer);



但这根本不显示任何内容!!!有任何想法吗?



But this does not display anything at all!!! Any ideas?

推荐答案

您根本不需要使用atoi. CString :: AppendFormat或CString :: Format确实可以完成您想做的事情:

You don''t need to use atoi at all. CString::AppendFormat or CString::Format does exactly what you want to do:

int i = 4711;
CString str;
str.Format (_T("My variable has value: %d\n"), i);



CString的格式化能力是最好的,而且非常灵活.



CString''s formatting capacity is one of the best and very flexible.


这是一个技巧吗?您正在混合字符和宽字符.一方面,您使用_ttoi和_T(),然后使用char和itoa.而且您可能使用UNICODE字符集进行构建.

像这样使它起作用.
If this a trick question? You''re mixing chars and wide chars. On one hand you use _ttoi and _T() and then char and itoa. And you probably build with UNICODE char set.

Make it like this and it will work.
int i;
TCHAR buffer[20];
CString str;

i= _ttoi(myVal->GetText());  //convert string to int
_itot(i, buffer, 10); //convert int back to string

str.AppendFormat(_T("Value is : %s\n"), buffer);



但是,为什么要完全使用itoa?只需使用已经指出的CString :: AppendFormat()或CString :: Format().



But why do you use itoa at all? Simply use CString::AppendFormat() or CString::Format() as already pointed out.


您如何显示" str?
how are you "displaying" str ?


这篇关于将int转换为字符串并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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