wince下的 类型转换(未完陆续编辑中)

 1.CString 转char 或wchat_t  再转 double,int ,wchar_t

在wince下要实现CString转换到double 类型,原以为double _wtof(const wchar_t *str ),即可结果发现

错误代码 error C3861: '_wtof': identifier not found(VS2005中编译)

原来好使是的呀,才知道CE不支持这个函数。只好用

int sscanf( const char *buffer, const char *format [, argument ] ... );
int swscanf( const wchar_t
*buffer, const wchar_t *format [, argument ] ... );

来实现,第一个参数是字符串地址,第二个参数是转换格式,最后则是将要把字符串转换成的类型(如:浮点,整形等)。

MSDN例程如下:


#include <stdio.h>
void main( void )
{
char  tokenstring[] = "15 12 14...";
char  s[81];
char  c;
int   i;
float fp;

/* Input various data from tokenstring: */
sscanf( tokenstring, "%s", s );
sscanf( tokenstring, "%c", &c );
sscanf( tokenstring, "%d", &i );
sscanf( tokenstring, "%f", &fp );

/* Output the data read */
printf( "String    = %s/n", s );
printf( "Character = %c/n", c );
printf( "Integer:  = %d/n", i );
printf( "Real:     = %f/n", fp );
}
Output
String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000
CString 先通过LockData()获取wchar_t*或char * 指针然后通过swscanf或sscanf 获取float型变量。在转成double.类型即可,别忘了给CString 类型使用UnLockData()。

同理使用swscanf 可以将CString 转换成int float wchar_t* 等类型。

也可使用如atoi,atof _wtoi,_wtof转换(在操作系统支持的情况下如winxp)

2.wchar_t与char转换

将wchar_t* 转成char*的实现函数如下:
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength=
wcslen(pwstr);
//获取转换后的长度

int nbytes = WideCharToMultiByte( 0// specify the code page used to perform the conversion
0,         // no special flags to handle unmapped characters
pwstr,     // wide character string to convert
nlength,   // the number of wide characters in that string
NULL,      // no output buffer given, we just want to know how long it needs to be
0,
NULL,      
// no replacement character given

NULL );    // we don't want to know if a character didn't make it through the translation
// make sure the buffer is big enough for this, making it larger if necessary

if(nbytes>len)   nbytes=len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符

WideCharToMultiByte( 0// specify the code page used to perform the conversion
0,         // no special flags to handle unmapped characters
pwstr,   // wide character string to convert
nlength,   // the number of wide characters in that string
pcstr, // put the output ascii characters at the end of the buffer
nbytes,                           // there is at least this much space there
NULL,      // no replacement character given
NULL );
return
 pcstr ;
}

 //将char* 转成wchar_t*的实现函数如下:
//这是把asii字符转换为unicode字符,和上面相同的原理

void c2w(wchar_t *pwstr,size_t len,const char *str)
{
if
(str)
 {
      size_t nu 
=
 strlen(str);
      size_t n 
=(size_t)multibytetowidechar(cp_acp,0,(const char *)str,(int)nu,null,0
);
      
if(n>=len)n=len-1
;
      multibytetowidechar(cp_acp,
0,(const char *)str,(int)nu,pwstr,(int
)n);
    pwstr[n]
=0
;
    }
}
3.使用
字符转浮点 unicode wcstod  ASCII strtod  通用_tcstod
整形              wcstol         strtol      _tcstol
看明白了吗??
unicode 的全是wcs  ASCII 是str 通用是_tcs
呵呵不只于此哦!像strlen wcslen _tclen等等..你熟悉的ASCII函数全有对应哦!
还有上文提到的   s
scanf   swscanf _tcscanf
              printf  wprintf _tcprintf
看到了吗   呵呵
unicode 是加了w的 通用是加_tc
所以为了简单你就记通用的就可以了

相关知识点:

Unicode的出现是为了适应软件国际化的需要。Unicode不同于双字节字符集(DBCS)。

一、相关操作函数

       1、DBCS使用下面的函数操作字符串:

             CharNext——获得后一个字符

            CharPrev——获得前一个字符

            IsDBCSLeadByte——判断是否为两个字节字符的第一个字节

            C++运行期库提供了以"_mbs"开头的一系列的函数操作DBCS。类似的函数有_mbscat等。

       2、ANSI字符集是一个美国标准。C++运行期库提供了以"str"开头的一些列的函数操作此字符集。

       3、C++运行期库为Unicode字符集提供了一系列以"wcs"开头的函数。

二、对应的数据类型

       1、对于ANSI字符定义为char。

        2、对于Unicode的字符定义为wchar_t。

三、使用环境

       1、首先要说明的是Win98对于Unicode的支持是很微弱的,所以如果要在Win98上运行Unicode编译的程序,可能造成运行错误或者失败。

       2、由于Win2000及以后的OS的内核都是使用Unicode编写的,所以虽然可以在其上运行ANSI编码的程序,但是其运行过程中很多地方都需要将 ANSI转换为Unicode以后,调用Unicode版本的函数,因为这个转换的过程存在所以ANSI的程序运行效率不高。在Win2000上最好使用 Unicode编写程序。

四、编写通用的程序

       1、在编程的时候使用TCHAR数据类型,此类型能够根据预编译宏的定义,将其转换为ANSI或者是Unicode。

       2、预编译宏_MBCS、_UNICODE和UNICODE。_MBCS是多字节和ANSI字符串的编译宏。此时TCHAR将转换为char。_UNICODE和UNICODE是Unicode编码的预编译宏,TCHAR将转换为wchar_t。

       3、_UNICODE和UNICODE与_MBCS不能在编译的时候同时被定义。

       4、_UNICODE宏用于C运行期库的头文件,UNICODE宏用于Windows头文件。一般同时定义这两个宏。

五、转换函数

       1、Unicode转换为ANSI使用:MultiByteToWideChar。

       2、ANSI转换为Unicode使用:WideCharToMultiByte。

宽字符转多字符:

       size_t wcstombs(char *mbstr, const wchar_t *wcstr, size_t count );

多字符转宽字符:

       size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count );

       另:L"ab"是C/C++标准宏,使用上是没有问题的

      1、client 里有些函数接口需要unicode,这些由于资源也在本地,可以直接使用MultiByteToWideChar或者mbstowcs+setlocale 转换

       2、对于需要从中文client->服务器->韩文client的方式下,在传文本的情况下,需要将文字的语言代码一起传出去,在接受端可以使用指定的代码,转换。服务器如有必要的话,也可以使用该代码转换,这样就可以在client上同时显示多国语言了

mbstowcs,wcstombs,WideCharToMultiByte,MultiByteToWideChar

一直以来,对这四个函数,很困惑。觉得没什么区别,微软为什么要搞两套出来。直到今天,才明白一点。但也不是很明白,仍有迷惑。

   简单的说。

   从功能上讲,基本相同。但WideCharToMultiByte()和MultiByteToWideChar()用于wince5.0以上。是win api.自适用中文。

   mbstowcs()函数和wcstombs()函数,选自CRT库。平台无关,但需设定locale。不设定Local,中文会乱码。如

#include <string>
#include <locale.h>
using namespace std;
string ws2s(const wstring& ws)
{
string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";获得当前的 Local

setlocale(LC_ALL, "chs");

const wchar_t* _Source = ws.c_str();
。。。
wcstombs(_Dest,_Source,_Dsize);
。。。

setlocale(LC_ALL, curLocale.c_str()); // 恢复Local

return result;

}

 

以上是我在开发一个小程序碰到的问题时找到的文章,由于对于在c++里string 和wstring的转换,开始我的理解只是单双字节的问题,后来发现这个和local是相关的。
现在我的理解是对于unicode,它的形式是双字节,但是要对这些双字节做出正确的解释,是需要正确的page页的,比如:setlocale(LC_ALL, "C"),只是以ansi页来解释,而setlocale(LC_ALL, "chs")是以gbk页码来翻译为中文的双字节。而过程就是mbstowcs,这个单字节转双字节函数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值