char to WCHAR

This is a discussion on char to WCHAR within the Windows Programming forums, part of the Platform Specific Boards category; Hey, is there any function to convert a char to a WCHAR? I can't get the speach function to work ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    112

    char to WCHAR

    Hey, is there any function to convert a char to a WCHAR? I can't get the speach function to work because it wants a WCHAR. I've tried type casting but it doesn't work. I can hard code the file in but i want the user to be able to choose which file they want. To hard code it I have to add an L infront of the quation marks for the text, but how could i do something like this with a normal char?

    The code below doesn't compile and gives me the following error:
    cannot convert parameter 1 from 'char [255]' to 'const unsigned short *
    Code:
    #include <sapi.h>
    #include <iostream.h>
    #include <fstream.h>
    
    int main(int argc, char* argv[])
    {
    	ifstream file;
    	char string[255];
        ISpVoice * pVoice = NULL;
    
        if (FAILED(::CoInitialize(NULL)))
            return 0;
    
    	cout << "Enter the name of a text file you want spoken: ";
    	cin  >> string;
    
        HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
        if( SUCCEEDED( hr ) )
        {
            hr = pVoice->Speak(string, SPF_IS_FILENAME | SPF_IS_XML, NULL);
            pVoice->Release();
            pVoice = NULL;
        }
    
        ::CoUninitialize();
        return 0;
    }
    Last edited by pinkcheese; 07-26-2002 at 09:27 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is what you are looking for.

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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    ahhh i don't think so...
    if i put the following code in it will compile:
    hr = pVoice->Speak(L"Hi how are you?", SPF_IS_FILENAME | SPF_IS_XML, NULL);

    but if i do something like this it won't:
    char string[150] = "hi how are you?";
    hr = pVoice->Speak(string, SPF_IS_FILENAME | SPF_IS_XML, NULL);

    I need to have an L infront of the quotations for it to work. How can i do that with a char?

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Surely if you are using ASCII, then the first 255 members of the UNICODE set have the same numerical value, so why can't you simply create a 16 bit array and copy the values of the ASCII string into it?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *gasp* Okay, the function that I posted would work like this:

    Code:
    char string[150] = "How are you?";
    TCHAR wstring[150];
    
    MultiByteToWideChar(CP_ACP, 0, string, -1,  wstring, 150);
    Puting the L in front of the quotes (or using the _T() or TEXT() macros) tells the compiler that the string is a wide character string. It isn't really anything special beyond that. The first part of you code worked because you were passing the correct type of string. The second didn't work because you where passing in an ascii string.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    112
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21