请问如何把CString类型的字符串转换成int型的数据

xiepeng 2006-11-29 11:46:15
请问如何把CString类型的字符串转换成int型的数据?
就是说我现在保存的是CStringArray类型,我想把其中的每一个字符串转换成数据存储到一个矩阵里,谢谢
...全文
559 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenteanet 2006-11-29
  • 打赏
  • 举报
回复
先用String里的c_ptr把string对象变为const char*,然后用下面的一些函数可以转换。
/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
jixingzhong 2006-11-29
  • 打赏
  • 举报
回复
CString 型转化成 int 型

  把 CString 类型的数据转化成整数类型最简单的方法就是使用标准的字符串到整数转换例程。
  虽然通常你怀疑使用_atoi()函数是一个好的选择,它也很少会是一个正确的选择。如果你准备使用 Unicode 字符,你应该用_ttoi(),它在 ANSI 编码系统中被编译成_atoi(),而在 Unicode 编码系统中编译成_wtoi()。你也可以考虑使用_tcstoul()或者_tcstol(),它们都能把字符串转化成任意进制的长整数(如二进制、八进制、十进制或十六进制),不同点在于前者转化后的数据是无符号的(unsigned),而后者相反。看下面的例子:

CString hex = _T("FAB");
CString decimal = _T("4011");
ASSERT(_tcstoul(hex, 0, 16) == _ttoi(decimal));
c4pt0r 2006-11-29
  • 打赏
  • 举报
回复
先用String里的c_ptr把string对象变为const char*
然后使用atoi
KUCHIBUE 2006-11-29
  • 打赏
  • 举报
回复
atoi
greenteanet 2006-11-29
  • 打赏
  • 举报
回复
试试考虑另外一个变量来保存,就是你把你的输入情况保存到另外一个变量中,这样子你就可以把这个全局变量在另外一个函数中访问啦。
xiepeng 2006-11-29
  • 打赏
  • 举报
回复
但是如果不清空的话,它的输入时会出错的。比如说,第一次输入1 2 3,第二次输入4 5 6,我是想得到1 2 3 4 5 6,但是如果不清空,就会是1 2 3 1 2 3 4 5 6,该怎么办呢,谢谢
greenteanet 2006-11-29
  • 打赏
  • 举报
回复
m_strArray.RemoveAll();
----------------------------
你这边不是清空了嘛?这样,OnButton2中int m_nSize = m_strArray.GetSize();这个应该就返回0了吧。
xiepeng 2006-11-29
  • 打赏
  • 举报
回复
我把代码贴出来,请大家看看:
这是写入部分:
void CFDlg::OnButton1()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString direct_str;
int x, y, z;
m_strArray.Add(m_str1);
m_strArray.Add(m_str2);
m_strArray.Add(m_str3);
///////////////创建一文件
CFile file("6.txt",CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
file.SeekToEnd();
for (int i=0;i<m_strArray.GetSize();i++)
{
direct_str=m_strArray.GetAt(i);
file.Write(direct_str.GetBuffer(0),direct_str.GetLength());

}
m_strArray.RemoveAll();
file.Close();
}
好了,现在是写进去了,我想把它读出来。原先我是想通过文件来读,后来我想既然CStringArray的strArray里已经存进去数了,不如直接放数组里算了,代码是这样的:
void CFDlg::OnButton2()
{
// TODO: Add your control notification handler code here
unsigned int myarray[40];
int m_nSize = m_strArray.GetSize();
for(int i=0; i<m_nSize; i++)
{
myarray[i]=atoi(m_strArray[i]);
}

}
可是在执行int m_nSize = m_strArray.GetSize();时,m_nSize是0,这样就直接跳出了,请问可能是什么原因,应该如何改?谢谢

64,639

社区成员

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

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