Re: WCHAR conversion problem



Hi,

Iñaki schrieb:

I need to convert a WCHAR[] to a char* but I don't know how can I do that. I'm not c++ expert... and I'm using an API with WCHARs...

It seems noone has hasked the most obvious questions yet:

1. What is the expected encoding of the char* you want to convert to?
2. Why do you need to convert anyway?

The background:
1. The times where you could assume that a char* is just a char* in terms of an ASCII string are long over. People use(d) tens/hundreds of different encodings for 7 or 8 bit character strings. The multibyte encoding from the mb* string functions is only one of many (I have never used it, I believe it was used for some asian countries but I might be wrong). That means: There is no such thing as plain text, and if you ask for text formats or conversins you always need to say what encoding you want to use.

2. For more than ten years now, Windows supports the unicode character set (WCHAR), and since Windows 98 is dead there is no need to use something else, ecxept for some very rare (or very old and not-yet-converted) applications. You ask for char*, so you seem to work on one of these. Is this true, or do you just don't know about Unicode?

What I need to convert is fileName of the following struct to a char*:

typedef struct _FbwfCacheDetail {
ULONG cacheSize; // size of cache used by the file
ULONG openHandleCount; // number of currently opened handles
ULONG fileNameLength; // file name length in bytes
WCHAR fileName[1]; // file name (may not be null-terminated)
} FbwfCacheDetail, *PFbwfCacheDetail;

I've tried the following:
char *FileName = (char *)malloc( MAX_FILENAME_LENGTH );
....
wcstombs_s(&wcharsConverted,FileName,MAX_FILENAME_LENGTH,Cache_detail->fileName,MAX_FILENAME_LENGTH);

where Cache_detail->fileName is suposed to contain fileName[], but FileName is empty while wcharsConverted seems to be OK.

Do you really need to convert to "multibyte" character set, or do you really mean ANSI or UTF-8 encoding?

Norbert
.



Relevant Pages

  • new String ( byte[] , encoding ) under the hood
    ... I was curious how new String (byte, encoding) could guess the ... char[] the right size. ... Going the other way String -> byte uses similar logic, but the buffer ...
    (comp.lang.java.programmer)
  • Re: lost mysql root password
    ... Not 02, but $20's, eg an ascii space char. ... this is encoding related. ... the database reserves multiple bytes ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: lost mysql root password
    ... On Friday 03 March 2006 04:38 pm, Gene Heskett wrote: ... Not 02, but $20's, eg an ascii space char. ... this is encoding related. ... the database reserves multiple bytes ...
    (Debian-User)
  • Re: Counting the number of bytes used by characters of a String
    ... or in a particular encoding? ... > I think a String class stores a string as an array of char, ... > know what place this array occupies. ... It's not actually an array of char itself - string is a very unusual ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: convert (single) char to string
    ... It is hard to find information on converting a char ... into a string, since most people ask how to convert charto string. ... Using encoding to convert a char to a string is not correct. ... Then you will be converting the char into a byte and treating that byte as if it was encoded data that you can decode into a string. ...
    (microsoft.public.dotnet.languages.csharp)