#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char *p="ab";
wchar_t c=0;
c=p[0];
c=c<<8;
c=c|p[1];
cout<<c<<endl; //char类型字符串转换成wchar_t类型并输出
wchar_t e='ab'; //对比检验
cout<<e<<endl;
char *a=new char[3];
a[0]=c>>8;
a[1]=c;
a[2]='\0';
cout<<a<<endl; //wchar_t类型转换成char类型字符串并输出
return 1;
}
现在将程序稍微改动一下,将输入的字符修改成'你',则运行后出现异常情况
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char *p="你";
wchar_t c=0;
c=p[0];
c=c<<8;
c=c|p[1];
cout<<c<<endl; //char类型字符串转换成wchar_t类型并输出
wchar_t e='你'; //对比检验
cout<<e<<endl;
char *a=new char[3];
a[0]=c>>8;
a[1]=c;
a[2]='\0';
cout<<a<<endl; //wchar_t类型转换成char类型字符串并输出
return 1;
}
请各位帮忙查看一下问题所在,谢谢!
6 个解决方案
#1
wchar_t输出好像要用wcout吧。
#2
mbstowcs
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count );
Routine Required Header Compatibility
mbstowcs <stdlib.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
If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
Parameters
wcstr
The address of a sequence of wide characters
mbstr
The address of a sequence of multibyte characters
count
The number of multibyte characters to convert
Remarks
The mbstowcs function converts count or fewer multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale. It stores the resulting wide-character string at the address represented by wcstr. The result is similiar to a series of calls to mbtowc. If mbstowcs encounters the single-byte null character ('\0') either before or when count occurs, it converts the null character to a wide-character null character (L'\0') and stops. Thus the wide-character string at wcstr is null-terminated only if a null character is encountered during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior is undefined.
If the wcstr argument is NULL, mbstowcs returns the required size of the destination string.
Example
/* MBSTOWCS.CPP illustrates the behavior of the mbstowcs function
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int i;
char *pmbnull = NULL;
char *pmbhello = (char *)malloc( MB_CUR_MAX );
wchar_t *pwchello = L"Hi";
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
printf( "Convert to multibyte string:\n" );
i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " multibyte character: %#.4x\n\n", pmbhello );
printf( "Convert back to wide-character string:\n" );
i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " wide character: %#.4x\n\n", pwc );
}
Output
Convert to multibyte string:
Characters converted: 1
Hex value of first multibyte character: 0x0e1a
Convert back to wide-character string:
Characters converted: 1
Hex value of first wide character: 0x0e1e
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count );
Routine Required Header Compatibility
mbstowcs <stdlib.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
If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
Parameters
wcstr
The address of a sequence of wide characters
mbstr
The address of a sequence of multibyte characters
count
The number of multibyte characters to convert
Remarks
The mbstowcs function converts count or fewer multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale. It stores the resulting wide-character string at the address represented by wcstr. The result is similiar to a series of calls to mbtowc. If mbstowcs encounters the single-byte null character ('\0') either before or when count occurs, it converts the null character to a wide-character null character (L'\0') and stops. Thus the wide-character string at wcstr is null-terminated only if a null character is encountered during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior is undefined.
If the wcstr argument is NULL, mbstowcs returns the required size of the destination string.
Example
/* MBSTOWCS.CPP illustrates the behavior of the mbstowcs function
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int i;
char *pmbnull = NULL;
char *pmbhello = (char *)malloc( MB_CUR_MAX );
wchar_t *pwchello = L"Hi";
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
printf( "Convert to multibyte string:\n" );
i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " multibyte character: %#.4x\n\n", pmbhello );
printf( "Convert back to wide-character string:\n" );
i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " wide character: %#.4x\n\n", pwc );
}
Output
Convert to multibyte string:
Characters converted: 1
Hex value of first multibyte character: 0x0e1a
Convert back to wide-character string:
Characters converted: 1
Hex value of first wide character: 0x0e1e
#3
使用wcstombs函数依然无法解决汉字不能正确输出的问题
代码如下,输出结果是乱码.
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
wchar_t *a=L"你gggff";
char *p=new char[50];
int i=wcstombs(p,a,50);
cout<<p<<endl;
return 1;
}
代码如下,输出结果是乱码.
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
wchar_t *a=L"你gggff";
char *p=new char[50];
int i=wcstombs(p,a,50);
cout<<p<<endl;
return 1;
}
#4
你的系统当前字符编码不一定是函数适用的转换编码。比如UTF-8,UTF-16或者GB2312等在转换的时候
就有不同的算法,你要先弄清楚系统的库要求的是什么编码,你的平台用的是什么编码。
就有不同的算法,你要先弄清楚系统的库要求的是什么编码,你的平台用的是什么编码。
#5
我用的windows xp
vc6.0环境
在类型转换后,输出英文字符没有问题
就是不能输出汉字
vc6.0环境
在类型转换后,输出英文字符没有问题
就是不能输出汉字
#6
如果你没有特殊的需要(严格区分是否为unicode)
就可以直接 char*
===================
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
char *a="你gggff";
cout<<a<<endl;
return 1;
}
就可以直接 char*
===================
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
char *a="你gggff";
cout<<a<<endl;
return 1;
}
#1
wchar_t输出好像要用wcout吧。
#2
mbstowcs
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count );
Routine Required Header Compatibility
mbstowcs <stdlib.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
If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
Parameters
wcstr
The address of a sequence of wide characters
mbstr
The address of a sequence of multibyte characters
count
The number of multibyte characters to convert
Remarks
The mbstowcs function converts count or fewer multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale. It stores the resulting wide-character string at the address represented by wcstr. The result is similiar to a series of calls to mbtowc. If mbstowcs encounters the single-byte null character ('\0') either before or when count occurs, it converts the null character to a wide-character null character (L'\0') and stops. Thus the wide-character string at wcstr is null-terminated only if a null character is encountered during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior is undefined.
If the wcstr argument is NULL, mbstowcs returns the required size of the destination string.
Example
/* MBSTOWCS.CPP illustrates the behavior of the mbstowcs function
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int i;
char *pmbnull = NULL;
char *pmbhello = (char *)malloc( MB_CUR_MAX );
wchar_t *pwchello = L"Hi";
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
printf( "Convert to multibyte string:\n" );
i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " multibyte character: %#.4x\n\n", pmbhello );
printf( "Convert back to wide-character string:\n" );
i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " wide character: %#.4x\n\n", pwc );
}
Output
Convert to multibyte string:
Characters converted: 1
Hex value of first multibyte character: 0x0e1a
Convert back to wide-character string:
Characters converted: 1
Hex value of first wide character: 0x0e1e
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count );
Routine Required Header Compatibility
mbstowcs <stdlib.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
If mbstowcs successfully converts the source string, it returns the number of converted multibyte characters. If the wcstr argument is NULL, the function returns the required size of the destination string. If mbstowcs encounters an invalid multibyte character, it returns –1. If the return value is count, the wide-character string is not null-terminated.
Parameters
wcstr
The address of a sequence of wide characters
mbstr
The address of a sequence of multibyte characters
count
The number of multibyte characters to convert
Remarks
The mbstowcs function converts count or fewer multibyte characters pointed to by mbstr to a string of corresponding wide characters that are determined by the current locale. It stores the resulting wide-character string at the address represented by wcstr. The result is similiar to a series of calls to mbtowc. If mbstowcs encounters the single-byte null character ('\0') either before or when count occurs, it converts the null character to a wide-character null character (L'\0') and stops. Thus the wide-character string at wcstr is null-terminated only if a null character is encountered during conversion. If the sequences pointed to by wcstr and mbstr overlap, the behavior is undefined.
If the wcstr argument is NULL, mbstowcs returns the required size of the destination string.
Example
/* MBSTOWCS.CPP illustrates the behavior of the mbstowcs function
*/
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
int i;
char *pmbnull = NULL;
char *pmbhello = (char *)malloc( MB_CUR_MAX );
wchar_t *pwchello = L"Hi";
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
printf( "Convert to multibyte string:\n" );
i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " multibyte character: %#.4x\n\n", pmbhello );
printf( "Convert back to wide-character string:\n" );
i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tHex value of first" );
printf( " wide character: %#.4x\n\n", pwc );
}
Output
Convert to multibyte string:
Characters converted: 1
Hex value of first multibyte character: 0x0e1a
Convert back to wide-character string:
Characters converted: 1
Hex value of first wide character: 0x0e1e
#3
使用wcstombs函数依然无法解决汉字不能正确输出的问题
代码如下,输出结果是乱码.
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
wchar_t *a=L"你gggff";
char *p=new char[50];
int i=wcstombs(p,a,50);
cout<<p<<endl;
return 1;
}
代码如下,输出结果是乱码.
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
wchar_t *a=L"你gggff";
char *p=new char[50];
int i=wcstombs(p,a,50);
cout<<p<<endl;
return 1;
}
#4
你的系统当前字符编码不一定是函数适用的转换编码。比如UTF-8,UTF-16或者GB2312等在转换的时候
就有不同的算法,你要先弄清楚系统的库要求的是什么编码,你的平台用的是什么编码。
就有不同的算法,你要先弄清楚系统的库要求的是什么编码,你的平台用的是什么编码。
#5
我用的windows xp
vc6.0环境
在类型转换后,输出英文字符没有问题
就是不能输出汉字
vc6.0环境
在类型转换后,输出英文字符没有问题
就是不能输出汉字
#6
如果你没有特殊的需要(严格区分是否为unicode)
就可以直接 char*
===================
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
char *a="你gggff";
cout<<a<<endl;
return 1;
}
就可以直接 char*
===================
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
char *a="你gggff";
cout<<a<<endl;
return 1;
}