字符串的转换问题及赋值等等的问题确实很烦人,这里对遇到的问题总结一下
一、unicode下字符串格式化
WCHAR tempStr[100]=L""; swprintf(tempStr,L"今年我%d岁了",25);结果:今年我25岁了
一、ASCII与UNICODE间字符串转换
ASCII->UNICODE
方法一(MultiByteToWideChar):
char temp[]="示例文本"; WCHAR string[256]; int iStrlen=sizeof(temp); MultiByteToWideChar(CP_ACP,0,(LPCSTR)(temp),iStrlen,string,iStrlen);方法二(A2W):
char temp[]="示例文本"; USES_CONVERSION; LPWSTR string A2W(temp);UNICODE ->ASCII
方法一(MultiByteToWideChar):
WCHAR string[]=L"示例文本"; char szAnsiStr[100]={0}; WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,string,-1,szAnsiStr,sizeof(szAnsiStr),NULL,NULL);方法二(W2A):
WCHAR string[]=L"示例文本"; char szAnsiStr[100]={0}; USES_CONVERSION; szAnsiStr=W2A(string);//代码有错误,用的时候查查W2A使用方法;
一、TCHAR 字符串数组的赋值memcpy、ZeroMemory
memcpy:一般的赋值方法
TCHAR szBuf[MAX_PATH] = _T("haha"); //初始化时才能用 memcpy(szBuf,_T("haha"),sizeof(_T("haha"))); //一般的赋值方法 //清空字符串 memcpy(szBuf,0,0);//这个与定义时赋空是一样的-》TCHAR szBuf[MAX_PATH]={0};
ZeroMemory:将指定内存空间清空
TCHAR *path=new TCHAR[MAX_PATH]; ZeroMemory(path,MAX_PATH);//清空内存 GetCurrentDirectory(MAX_PATH,path);//获取当前应用程序的地址一篇文章: http://hi.baidu.com/peidun/item/3f19114de1fc6033fb896037
二、复制、追加字符串(wcscpy、wcscat || strcpy、strcat)
char *strcpy( char *strDestination, const char *strSource ); wchar_t *wcscpy( wchar_t *strDestination, const wchar_t *strSource );
wcscpy:是strcpy的宽字符形式,strcpy用于ASCII编码,wcscpy用于UNICODE编码。
作用:将一个字符串复制到目标字符串中;
char *strcat( char *strDestination, const char *strSource ); wchar_t *wcscat( wchar_t *strDestination, const wchar_t *strSource );wcscat:同样是strcat的宽字符形式,strcat用于ASCII编码,wcscat用于UNICODE编码;
示例:
#include <string.h> #include <stdio.h> void main( void ) { char string[80]; strcpy( string, "Hello world from " ); strcat( string, "strcpy " ); strcat( string, "and " ); strcat( string, "strcat!" ); printf( "String = %s\n", string ); }结果:
String = Hello world from strcpy and strcat!
三、字符串比较strcmp、wcscmp || _stricmp、_wcsicmpint strcmp( const char *string1, const char *string2 );//ASCII形式 int wcscmp( const wchar_t *string1, const wchar_t *string2 );//UNICODE形式这个函数直接将两个字符串逐个字符地按照ASCII码顺序对比,所以对于string1="quick",string2="Quick",string1>string2;
示例:
#include "stdafx.h" #include <string.h> #include <stdio.h> #include <Windows.h> char string1[] = "The quick brown dog jumps over the lazy fox"; char string2[] = "The QUICK brown dog jumps over the lazy fox"; int _tmain(int argc, _TCHAR* argv[]) { char tmp[20]; int result; result = strcmp( string1, string2 ); if( result > 0 ) strcpy( tmp, "greater than" ); else if( result < 0 ) strcpy( tmp, "less than" ); else strcpy( tmp, "equal to" ); printf( "\tstrcmp: String 1 is %s string 2\n", tmp ); //调用windows系统函数 Sleep(10000); return 0; }
int _stricmp( const char *string1, const char *string2 );//ASCII形式 int _wcsicmp( const wchar_t *string1, const wchar_t *string2 );//UNICODE形式这两个函数同样是对比两字符串的函数,但他们在对比字符串前, 会把两字符串全部转换为小写形式后再对比,换句话说,这两个函数只比较字母,不比较大小写;结果与
#include "stdafx.h" #include <string.h> #include <stdio.h> #include <Windows.h> char string1[] = "The quick brown dog jumps over the lazy fox"; char string2[] = "The QUICK brown dog jumps over the lazy fox"; int _tmain(int argc, _TCHAR* argv[]) { char tmp[20]; int result; result = _stricmp( string1, string2 ); if( result > 0 ) strcpy( tmp, "greater than" ); else if( result < 0 ) strcpy( tmp, "less than" ); else strcpy( tmp, "equal to" ); printf( "\t_stricmp: String 1 is %s string 2\n", tmp ); Sleep(10000); return true; }