convertion between two types

closed account (28poGNh0)
Please how can I convert from WCHAR to string type ?

thanks.
closed account (28poGNh0)
bump
closed account (2UD8vCM9)
Rafae just gave you an example of how to do that.

Since you didn't understand it, it would help if you could give us the code in which you're trying to convert from a WCHAR to a string so we can try to help you.
closed account (28poGNh0)
This is the code

1
2
3
4
5
6
7
8
9
10
11
12
# include <windows.h>
# include <string>

int main()
{
    WCHAR szBuffer[] = L"Convert this sz to string character";
    std::string str;

    str = szBuffer;/// Invalid conversion from WCHAR* to ...

    return 0;
}
Last edited on
closed account (2UD8vCM9)
Sorry for the slow response was putting food in the oven.

Okay I see what you're trying to do.
I think this should suffice for the way you're approaching it.

Note: This approach assumes that the WCHAR array ends with a null terminator ('\0') which it will always end with if properly terminated which your array is in the way it is declared.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <windows.h>
#include <string>
#include <iostream>

std::string WCHARtoString(WCHAR wcharBuffer[])
{
	std::string returnstring;
	for (unsigned int i = 0; wcharBuffer[i] != '\0'; i++)
	{
		returnstring += wcharBuffer[i];
	}
	return returnstring;
}

int main()
{
	WCHAR szBuffer[] = L"Convert this sz to string character";
	std::string str;

	str = WCHARtoString(szBuffer);
	std::cout << "String Converted:" << str << std::endl;
	return 0;
}


Let me know if this works for you! :D
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <codecvt>

int main()
{
    const wchar_t szBuffer[] = L"Convert this" ;
    const std::wstring wstr = L"and convert this too" ;

    // http://en.cppreference.com/w/cpp/locale/codecvt
    struct codecvt_w2c : public std::codecvt < wchar_t, char, std::mbstate_t > 
    { public: ~codecvt_w2c() {} }; // public destructor

    // http://en.cppreference.com/w/cpp/locale/wstring_convert
    std::wstring_convert<codecvt_w2c> converter( "*** conversion error ***" ) ;

    const std::string str = converter.to_bytes(szBuffer);
    std::cout << str << '\n' ;

    const std::string str2 = converter.to_bytes(wstr);
    std::cout << str2 << '\n' ;
}

http://coliru.stacked-crooked.com/a/e5b2a43c635f0a59

Or use std::wcstombs()
http://en.cppreference.com/w/cpp/string/multibyte/wcstombs
closed account (28poGNh0)
Thanks

I developed the same function as WCHARtoString() ,also TCHAR to strings ,strings to TCHAR .. bref I found myself forced to develop function to handle convertions between datatypes ,and i thought maybe this is not a good way to do so

anyone knows good tuto about data types convertion

@ Pindrought
could you implement ,for me, a new function StringToWCHAR ? thanks

hope for more replies
closed account (2UD8vCM9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <windows.h>
#include <string>
#include <iostream>

std::string WCHARtoString(WCHAR wcharBuffer[])
{
	std::string returnstring;
	for (unsigned int i = 0; wcharBuffer[i] != '\0'; i++)
	{
		returnstring += wcharBuffer[i];
	}
	return returnstring;
}

WCHAR * StringToWCHAR(std::string strbuffer)
{
	WCHAR * returnptr;
	returnptr = new WCHAR[strbuffer.size()+1]; //We are allocating memory in this line and this is why we need to call delete when we're done with our WCHAR ptr
	for (int i = 0; i < strbuffer.size(); i++)
	{
		returnptr[i] = strbuffer[i];
	}
	returnptr[strbuffer.size()] = '\0';
	return returnptr;
}

int main()
{
	WCHAR szBuffer[] = L"Convert this sz to string character";
	std::string str;

	str = WCHARtoString(szBuffer);
	std::cout << "String Converted:" << str << std::endl;

	WCHAR * strToWCharTest = StringToWCHAR(str);
	std::wcout << "String Converted to WChar* :" << strToWCharTest << std::endl;
	delete[] strToWCharTest; //Delete ptr to deallocate the memory that was allocated in the function

	return 0;
}



For this you will be using a WCHAR * instead of a WCHAR array, but they perform exactly the same when used correctly.

Also, keep in mind to output to the stream, we have to use std::wcout instead of std::cout since we are outputting wide characters.

Also, since we are allocating memory in our stringtoWCHAR function, we have to call delete on our pointer after we are done using our WCHAR * so that we don't have memory leaks.

Hopefully it makes sense in my example where I delete the ptr after I am done using it.
Last edited on
closed account (28poGNh0)
@ Pindrought
thanks

@ JLBorges
I m using vs 2008 why this error
fatal error C1083: Cannot open include file: 'codecvt'

also this header! not recongnized in code::blocks
should I add something ?
Last edited on
> I m using vs 2008 why this error
> fatal error C1083: Cannot open include file: 'codecvt'

Unicode support and <codecvt> came with C++11.

It does not make much sense to use such an old compiler. Switch to VS 2013 (IIRC, even VS 2010 has <codecvt>)


> also this header! not recongnized in code::blocks

The GNU library continues to be non-conforming in this regard.

It should have std::wcstombs() though; you could use that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
#include <string>

int main()
{
    const wchar_t wstr[] = L"Hello world!" ;

    char cstr[100] ;
    const std::size_t sz = std::wcstombs( cstr, wstr, sizeof(cstr) ) ;

    if( sz != -1 )  // if( sz != -1U ) // *** EDIT
    {
        const std::string str(cstr) ;
        std::cout << str << '\n';
    }

    else std::cerr << "**** conversion error ****\n" ;
}
Last edited on
closed account (28poGNh0)
Thanks a lot @JLBorges That's what I am looking for

one last thing
what this means -1U ,a zero byte memory !? :)
Last edited on
I actually have the same question: wouldn't -1U be threated as unsigned int and then expand to size_t filling most significant bytes with 0?
> wouldn't -1U be threated as unsigned int and then expand to size_t filling most significant bytes with 0?

Yes, thanks. It should be just if( sz != -1 ).
closed account (28poGNh0)
One last quest

It does not make much sense to use such an old compiler. Switch to VS 2013 (IIRC, even VS 2010 has <codecvt>)


can we not include it manualy ?
Last edited on
> can we not include it manualy ?

No. We have to use the standard headers that came with the implementation. With different compilers, or different versions of a compiler, it is technically a different implementation of the standard library.

Most C++11 headers would not even compile without errors if we try to use them in legacy C++ mode.
closed account (28poGNh0)
Thanks
Topic archived. No new replies allowed.