473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to convert wchar_t * to char *

How can I convert a wchar_t * to char *? My code is something like that but it just get the first character:

wchar_t * filename= L"C:\\test";
char* c = (char*)filename;
Aug 6 '07 #1
3 95139
JosAH
11,448 Expert 8TB
How can I convert a wchar_t * to char *? My code is something like that but it just get the first character:

wchar_t * filename= L"C:\\test";
char* c = (char*)filename;
Welcome to the wonderful world of Unicode and the non ASCII world (most of the
real world actually). A wchar_t is a 16 bit codepoint. Most likely codepoints are
stored in little endian order on your machine because you can read the first
'character' which happens to be the ASCII codepoint for an upper case C while
the hi byte equals zero.

Basically you can't just chop off the high byte from a wchar_t to get your char.
You have to decide how you want to encode those 16 bit codepoints to 8 bit
encoded bytes. I suggest UTF-8 for this.

Read all about it at the Unicode site.

best of luck and

kind regards,

Jos
Aug 6 '07 #2
afraze
18
petzold,

you can try this code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. int main(void)
  5. {
  6.     char *str;
  7.  
  8.     wchar_t array[] = L"Hello World";
  9.  
  10.     wcstombs(str, array, 12);
  11.  
  12.     std::cout << str;
  13.  
  14. }
Kind regards...
Aug 6 '07 #3
In the example above,
you must allocate memory for str array and memset it with zeros.

Expand|Select|Wrap|Line Numbers
  1. char *str = new char[12 + 1];
  2. memset( str, 0, 12 + 1);
  3. wchar_t array[] = L"Hello World";
  4. wcstombs(str, array, 12);
  5. std::cout << str;
  6. delete str;
Aug 5 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
4
by: Bren | last post by:
Hi all, I wonder if anyone knows of a string class out there that can do on demand conversion from wchar_t to char? We are writing an app that will need to be localized and cross platform. ...
27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
6
by: gerg | last post by:
How would one convert a wstring to a string? This is what I have so far: bool copyto(std::string& l,std::wstring& r) { bool ret = false; size_t i = 0; const size_t n = r.length()+1;
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
4
by: uday.sen | last post by:
Hi, I need to convert a string from UTF8 to wide character (wchar_t *). I perform the same in windows using: MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen); However, in linux...
1
by: Rui Maciel | last post by:
I'm trying to use the getwc() function to get individual characters from a stream but I've stumbled on a type conversion problem. The getwc() function returns a type wint_t instead of a wchar_t and...
4
by: =?ISO-8859-2?Q?Boris_Du=B9ek?= | last post by:
Hi, I have an API that returns UTF-8 encoded strings. I have a utf8 codevt facet available to do the conversion from UTF-8 to wchar_t encoding defined by the platform. I have no trouble...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.