工作笔记(一)

1.The GetCurrentDirectory function retrieves the current directory for the current process

 DWORD GetCurrentDirectory(  DWORD nBufferLength,  // size of directory buffer
  LPTSTR lpBuffer       // directory buffer);

用法:

 TCHAR tszModule[MAX_PATH ] = { 0 };
 ::GetCurrentDirectory(MAX_PATH, tszModule);

2.The GetModuleFileName function retrieves the fully qualified path for the specified module.

DWORD GetModuleFileName(  HMODULE hModule,    // handle to module
  LPTSTR lpFilename,  // path buffer  DWORD nSize         // size of buffer);

用法:

TCHAR tszModule[MAX_PATH + 1] = { 0 };
::GetModuleFileName(NULL, tszModule, MAX_PATH);

3. 

(1) char* 转到 wchar*

The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set.

int MultiByteToWideChar(  UINT CodePage,         // code page
  DWORD dwFlags,         // character-type options
  LPCSTR lpMultiByteStr, // string to map
  int cbMultiByte,       // number of bytes in string
  LPWSTR lpWideCharStr,  // wide-character buffer
  int cchWideChar        // size of buffer);

用法:

#define MAX_PATH          260

::MultiByteToWideChar(CP_ACP,0,(const char *)CStr,-1,tszModule,MAX_PATH);

CStr将被转换字符串的字符

-1指的是CStr以空字符终止

tszModule 指向接收被转换字符串的缓冲区

MAX_PATH 指向的缓冲区的宽字符个数

(2) wchar* 转 到 char*

方法一

  TCHAR tszModule[MAX_PATH ] = { 0 };
   ::GetCurrentDirectory(MAX_PATH, tszModule);

   size_t len = wcslen(tszModule) + 1;
   size_t converted = 0;
   char CStr[MAX_PATH];
   wcstombs_s(&converted, CStr, len, tszModule, _TRUNCATE);

 方法二

WideCharToMultiByte

int WideCharToMultiByte(
  UINT CodePage,            // code page
  DWORD dwFlags,            // performance and mapping flags
  LPCWSTR lpWideCharStr,    // wide-character string
  int cchWideChar,          // number of chars in string
  LPSTR lpMultiByteStr,     // buffer for new string
  int cbMultiByte,          // size of buffer
  LPCSTR lpDefaultChar,     // default for unmappable chars
  LPBOOL lpUsedDefaultChar  // set when default char used
);
lpWideCharStr:指向将被转换的unicode字符串。
cchWideChar:指定由参数lpWideCharStr指向的缓冲区的字符个数。如果这个值为-1,字符串将被设定为以NULL为结束符的字符串,并且自动计算长度。
lpMultiByteStr:指向接收被转换字符串的缓冲区。
cchMultiByte:指定由参数lpMultiByteStr指向的缓冲区最大值(用字节来计量)。若此值为零,函数返回lpMultiByteStr指向的目标缓冲区所必需的字节数,在这种情况下,lpMultiByteStr参数通常为NULL。
lpDefaultCharpfUsedDefaultChar:只有当WideCharToMultiByte函数遇到一个宽字节字符,而该字符在uCodePage参数标识的代码页中并没有它的表示法时,WideCharToMultiByte函数才使用这两个参数。如果宽字节字符不能被转换,该函数便使用lpDefaultChar参数指向的字符。如果该参数是NULL(这是大多数情况下的参数值),那么该函数使用系统的默认字符。该默认字符通常是个问号。这对于文件名来说是危险的,因为问号是个通配符。pfUsedDefaultChar参数指向一个布尔变量,如果Unicode字符串中至少有一个字符不能转换成等价多字节字符,那么函数就将该变量置为TRUE。如果所有字符均被成功地转换,那么该函数就将该变量置为FALSE。当函数返回以便检查宽字节字符串是否被成功地转换后,可以测试该变量。
返回值:如果函数运行成功,并且cchMultiByte不为零,返回值是由 lpMultiByteStr指向的缓冲区中写入的字节数;如果函数运行成功,并且cchMultiByte为零,返回值是接收到待转换字符串的缓冲区所必需的字节数。如果函数运行失败,返回值为零。若想获得更多错误信息,请调用GetLastError函数。

4.截取字符串函数

basic_string::substr
basic_string substr(size_type _Off = 0,size_type _Count = npos) const;
功能:复制子字符串,要求从指定位置开始,并具有指定的长度。
 
参数
_Off
所需的子字符串的起始位置。字符串中第一个字符的索引为 0,默认值为0.
_Count
复制的字符数目
返回值
一个子字符串,从其指定的位置开始

 多字节转换成宽字符

int MultiByteToWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cchMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar
);

函数功能:该函数映射一个字符串到一个宽字符(unicode)的字符串。由该函数映射的字符串没必要是多字节字符组。

lpMultiByteStr:指向将被转换字符串的字符。
cchMultiByte:指定由参数lpMultiByteStr指向的字符串中字节的个数。如果lpMultiByteStr指定的字符串以空字符终止,可以设置为-1(如果字符串不是以空字符中止,设置为-1可能失败,可能成功),此参数设置为0函数将失败。
lpWideCharStr:指向接收被转换字符串的缓冲区
cchWideChar:指定由参数lpWideCharStr指向的缓冲区的宽字符个数。若此值为零,函数返回缓冲区所必需的宽字符数,在这种情况下,lpWideCharStr中的缓冲区不被使用。
 
例子:
int UTF8ToString(LPCSTR lpSrc, CString& strDst, int nLength)
{
 LPTSTR lpDst = NULL;
 int count = ::MultiByteToWideChar(CP_UTF8, 0, lpSrc, nLength, NULL, 0);
 if (count <= 0)
 {
  return 0;
 }

 LPWSTR lpWide = new WCHAR[count + 1];
 memset(lpWide, 0, (count + 1) * sizeof(WCHAR));

 ::MultiByteToWideChar(CP_UTF8, 0, lpSrc, nLength, lpWide, count);

#ifdef _UNICODE
 lpDst = lpWide;
#else
 count = ::WideCharToMultiByte(::GetACP(), 0, lpWide, -1, NULL, 0, NULL, 0);

 if (count > 0)
 {
  lpDst = new char[count + 1];
  memset(lpDst, 0, count + 1);

  ::WideCharToMultiByte(::GetACP(), 0, lpWide, -1, lpDst, count, NULL, 0);
 }

 delete [] lpWide;
#endif

 strDst = lpDst;
 delete[] lpDst;
 return count;
}

 5.vs代码对齐快捷键

  选定代码后,按Ctrl+K+F组合键,可以自动进行代码对齐。注意:要先按下Ctrl和K,再按下F,因为Ctrl+F是查找的快捷键

6

char*转换为wchar_t*

stdlib.h中的mbstowcs_s函数,可以通过下面的例子了解其用法:

 

char *CStr = "string to convert";

size_t len = strlen(CStr) + 1;

size_t converted = 0;

wchar_t *WStr;

WStr=(wchar_t*)malloc(len*sizeof(wchar_t));

mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE);

 

其结果是WStr中储存了CStrwchar_t版本。

 

wchar_t*转换为char*

和上面的方法类似,用stdlib.h中的wcstombs_s函数,例子:

 

wchar_t *WStr = L"string to convert";

size_t len = wcslen(WStr) + 1;

size_t converted = 0;

char *CStr;

CStr=(char*)malloc(len*sizeof(char));

wcstombs_s(&converted, CStr, len, WStr, _TRUNCATE);

 

这时WStr中的内容将被转化为char版本储存在CStr中。

 

另外还可以通过流的方法来char*类型转换为wchar_t*类型,但这样的转换得到的结果将是const类型,而类似的方法不能将wchar_t*类型转换为char*类型。

 

把(constchar*转换为const wchar_t*

需要用到 sstream 头文件:

 

char *cstr="string to convert";

wstringstream wss;

wss<<cstr;

 

再调用wss.str().c_str(); 即可得到 const wchar_t* 类型的返回值。

 

虽然stringstream流不能将wchar_t*转换成char*,但可以用来进行数值类型和字符串之间的转换,例如:

 

double d=2734792.934f;

stringstream ss;

ss<<d;

 

调用ss.str()可得到string类型字符串 ”273479e+006”,又如:

 

string str("299792458");

stringstream ss;

long i=0;

ss<<str;

ss>>i;

 

此时i=299792458

lstrcpyW(LPWSTR lpString1,LPCWSTR lpString2);lpString1 为输出参数,lpString2 为输入参数  此函数 类似 sprintf函数 把wchar

原文地址:https://www.cnblogs.com/chechen/p/3791637.html