字符串转换为数字atoi函数,实参问题求解

C少 2011-11-19 03:22:39

void CTcpclientDlg::OnBnClickedConnet()
{

CString str,str1,worn;//定义字符串
int port;//定义端变量
GetDlgItem(IDC_ADDR)->GetWindowText(str); //获取地址
GetDlgItem(IDC_PORT)->GetWindowText(str1);//获取端口
if (str==""||str1=="")
{
worn="服务地址或端口号不能为空";
MessageBox(worn);
}
else
{
port=atoi(str1.GetBuffer(1));
addr.sin_family=AF_INET;
addr.sin_addr.S_un.S_addr=inet_addr(str.GetBuffer(1));

}

这里下划线的两处,atoi函数实参应为const char*,这里报错,原因是Lpwstr类型实参与const char*类型的形参不兼容,应该怎么修改?
...全文
419 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
C少 2011-11-19
  • 打赏
  • 举报
回复
我找到问题症结所在了,是要把工程属性设置为多字符集,项目-》属性-》左边的 配置属性-》字符集 ,选使用多字节字符集。还是先谢谢你,分都给你了
hongwenjun 2011-11-19
  • 打赏
  • 举报
回复
http://blog.csdn.net/misterliwei/article/details/2318725

inet_addr( const char *cp ) ; 没办法,只能把你哪个 宽字节转成 ansi 了
C少 2011-11-19
  • 打赏
  • 举报
回复
非常感谢,还有inet_addr()也是报同样的错,还得麻烦你,我的环境是win7 64(bit)vs2010
hongwenjun 2011-11-19
  • 打赏
  • 举报
回复
LPSTR 指向字符串的32位指针。
我猜你的 Lpwstr 应该是 指向宽字符串的32位指针。
使用这个函数试试,宽字节的版本
_wtoi
hongwenjun 2011-11-19
  • 打赏
  • 举报
回复
数据类型

本章列出了微软基础类库中最常见的数据类型。大多数数据类型是与Windows软件开发工具包(SDK)中一致的,也有一些是MFC独有的。
下面的数据类型是Windows SDK 和MFC共用的: · BOOL 布尔值。
· BSTR 32位字符指针。
· BYTE 8位无符号整数。
· COLORREF 用作颜色值的32位值。
· DWORD 32位无符号整数,或者是段地址以及与之相关的偏移量。
· LONG 32位带符号整数。
· LPARAM 32位值,作为参数传递给一个窗口过程或者回调函数。
· LPCSTR 指向字符串常量的32位指针。
· LPSTR 指向字符串的32位指针。
· LPCTSTR 指向一个兼容 Unicode 和 DBCS 的字符串的32位指针。
· LPTSTR 指向一个兼容 Unicode 和 DBCS 的字符串的32位指针。
· LPVOID 指向一个未指定类型的32位指针。
· LRESULT 窗口过程或者回调函数返回的32位值。
· UINT 在Windows 3.0和3.1中表示16位的无符号整数,在Win32中表示32位的无符号整数。
· WNDPROC 指向一个窗口过程的32位指针。
· WORD 16位无符号整数。
· WPARAM 作为参数传递给窗口函数或者回调函数的值:在Windows 3.0和3.1 中为16位,在Win32中为32位。

微软基础类库中独有的数据类型如下: · POSITION 用于标记集合中一个元素的位置的值;被MFC中的集合类所使用。
· LPCRECT 指向一个RECT结构体常量(不能修改)的32位指针。
在《Win32 SDK 程序员参考》的“数据类型”部分,有个不太通用的数据类型的列表。
hongwenjun 2011-11-19
  • 打赏
  • 举报
回复
int atoi( const char *string );

从 atoi 的原型 分析


CString
CString does not have a base class.

A CString object consists of a variable-length sequence of characters. CString provides functions and operators using a syntax similar to that of Basic. Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays.

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

When not using _UNICODE, CString is enabled for multibyte character sets (MBCS, also known as double-byte character sets, DBCS). Note that for MBCS strings, CString still counts, returns, and manipulates strings based on 8-bit characters, and your application must interpret MBCS lead and trail bytes itself.

CString objects also have the following characteristics:

CString objects can grow as a result of concatenation operations.


CString objects follow “value semantics.” Think of a CString object as an actual string, not as a pointer to a string.


You can freely substitute CString objects for const char* and LPCTSTR function arguments.


A conversion operator gives direct access to the string’s characters as a read-only array of characters (a C-style string).
C少 2011-11-19
  • 打赏
  • 举报
回复
port=atoi(str1.GetBuffer(1));
addr.sin_addr.S_un.S_addr=inet_addr(str.GetBuffer(1));

下划线部分

64,676

社区成员

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

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