码迷,mamicode.com
首页 > Windows程序 > 详细

windows下wchar_t* 转char*

时间:2014-12-29 12:02:19      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

这个在windows下很常见,常用,留个档。

一般用这个函数:

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

mbstr

The address of a sequence of multibyte characters.

wcstr

The address of a sequence of wide characters.

count

The maximum number of bytes that can be stored in the multibyte output string.

locale

The locale to use.

 

下面是我写的一个测试,是根据项目改过来的:

 1 #include <windows.h>
 2 #include <string.h>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <boost/filesystem.hpp>
 6 
 7 namespace bfs = boost::filesystem;
 8 
 9 
10 bool get_path(char *file_name,size_t size){
11 
12 
13 char ownPth[MAX_PATH];
14 
15 HMODULE hModule = GetModuleHandle(NULL);    // Will contain exe path,When passing NULL to GetModuleHandle, it returns handle of exe itself
16 if (hModule != NULL)
17 {
18 GetModuleFileNameA(hModule,ownPth, MAX_PATH);    // 
19 }
20 else
21 ownPth[0]=0;
22 
23 bfs::path iic_file(ownPth);
24 iic_file = iic_file.parent_path() / "dlp_usb.iic" ; //For releaseing
25 
26 if( !bfs::is_regular_file(iic_file))
27 {
28 printf("\r\nError,open 68013 iic file fail\r\n");
29 return false ;
30 }
31 
32 
33 wcstombs(file_name,iic_file.c_str(),size);
34 
35 //memcpy(file_name,iic_file.c_str(),byte_count);
36 
37 return true;
38 
39 }
40 
41 int main(){
42 
43 char iic_file[512];
44 
45 if(get_path( iic_file,512)){
46 
47 printf("TRUE\n");
48 }
49 
50 return 0;
51 }

 

 

 

 

references:

http://msdn.microsoft.com/en-us/library/5d7tc9zw.aspx

http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/

windows下wchar_t* 转char*

标签:

原文地址:http://www.cnblogs.com/foohack/p/4191274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!