inet_ntoa返回乱码

悠然的二货 2015-11-14 12:58:16
addrSrv.sin_addr.S_un.S_addr = inet_addr("192.168.0.2");
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);

wsprintf(szBuffer, TEXT("IP %s"), inet_ntoa(addrSrv.sin_addr));
运行结果: 㤱⸲㘱⸸⸰2몭몭몭몭몭
...全文
163 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhk_0424 2015-11-17
  • 打赏
  • 举报
回复
楼上正解,用sprintf
赵4老师 2015-11-17
  • 打赏
  • 举报
回复
sprintf, swprintf Write formatted data to a string. int sprintf( char *buffer, const char *format [, argument] ... ); int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... ); Routine Required Header Compatibility sprintf <stdio.h> ANSI, Win 95, Win NT swprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character. Parameters buffer Storage location for output format Format-control string argument Optional arguments For more information, see Format Specifications. Remarks The sprintf function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined. swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _stprintf sprintf sprintf swprintf Example /* SPRINTF.C: This program uses sprintf to format various * data and place them in the string named buffer. */ #include <stdio.h> void main( void ) { char buffer[200], s[] = "computer", c = 'l'; int i = 35, j; float fp = 1.7320534f; /* Format and print various data: */ j = sprintf( buffer, "\tString: %s\n", s ); j += sprintf( buffer + j, "\tCharacter: %c\n", c ); j += sprintf( buffer + j, "\tInteger: %d\n", i ); j += sprintf( buffer + j, "\tReal: %f\n", fp ); printf( "Output:\n%s\ncharacter count = %d\n", buffer, j ); } Output Output: String: computer Character: l Integer: 35 Real: 1.732053 character count = 71 Stream I/O Routines See Also _snprintf, fprintf, printf, scanf, sscanf, vprintf Functions
  • 打赏
  • 举报
回复
你如果用的是vc6,且默认的字符集为_MBCS,多字节字符集环境 那么你就不能用w开头的unicode专用函数,因为字符编码是互不兼容的 你在TEXT宏上按F12就能看到当前宏展开是什么情况 如果是没有定义UNICODE宏,那么是按如下逻辑展开 #define __TEXT(quote) quote 也就是不做任何处理 如果定义了UNICODE宏,则按 #define __TEXT(quote) L##quote 将你的代码套进来就是L"IP %s" 即为宽字符字符串常量 但你的编译环境显然是没有定义UNICODE宏的 则实际展开为"IP %s",即默认的多字节字符集环境
笨笨仔 2015-11-17
  • 打赏
  • 举报
回复
引用 楼主 a785214563 的回复:
addrSrv.sin_addr.S_un.S_addr = inet_addr("192.168.0.2");
		addrSrv.sin_family = AF_INET;
		addrSrv.sin_port = htons(6000);

		wsprintf(szBuffer, TEXT("IP %s"), inet_ntoa(addrSrv.sin_addr));
运行结果: 㤱⸲㘱⸸⸰2몭몭몭몭몭
这涉及到你收到的字符编码类型是否需要进行转换的问题。
zwfgdlc 2015-11-16
  • 打赏
  • 举报
回复
inet_ntoa返回的是char*类型 wsprintf是格式化UNICODE字符串的. 用inet_ntoa就用sprintf格式化字符串 一定要用wsprintf就用WSAAddressToString返回UNICODE编码IP字串
Magic丶旭 2015-11-16
  • 打赏
  • 举报
回复
先把缓冲区用0填充一下,看看是不是没有空终止

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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