TCHAR to string
-
Hi all, How can i convert a TCHAR to a string? for example:
TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); string pc; ... convert / assign the value of infoBuf to pc ...
Many Thanx
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi all, How can i convert a TCHAR to a string? for example:
TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); string pc; ... convert / assign the value of infoBuf to pc ...
Many Thanx
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi, There is no needed any conversion: pc.assign( bufCharCount ); should work fine.
----------- Mila
Sure about that ? Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'DWORD' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 -- modified at 3:49 Wednesday 13th December, 2006
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Sure about that ? Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'DWORD' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 -- modified at 3:49 Wednesday 13th December, 2006
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Sorry bro but: Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'TCHAR [32767]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24
TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); string pc; pc.assign( infoBuf ); // ERROR
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Sorry bro but: Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::assign(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'TCHAR [32767]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24
TCHAR infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); string pc; pc.assign( infoBuf ); // ERROR
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
ok, you use VS2005 (I've just noticed it). There is UNICODE by default. TCHAR is defined as a wchar_t and to cooperating with it is std::wsting. If you really need convet it, you can use e.g. wcstombs or W2A
----------- Mila
Thnx alot Mila for the help and quick replies .... :) ANSWER:
wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf;
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
ok, you use VS2005 (I've just noticed it). There is UNICODE by default. TCHAR is defined as a wchar_t and to cooperating with it is std::wsting. If you really need convet it, you can use e.g. wcstombs or W2A
----------- Mila
Sorry but I have another question ....:sigh: How do I now convert wstring to string, Cause see I have this function that accepts a std:string and not a std::wstring ... :confused: ERROR: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::wstring' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 Thanx in advance (again)
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Hi all, How can i convert a TCHAR to a string? for example:
TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); string pc; ... convert / assign the value of infoBuf to pc ...
Many Thanx
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
One more way to achieve this. Save one function call,too.
TCHAR infoBuf[INFO_BUFFER_SIZE];
GetComputerName( infoBuf, &bufCharCount );
basic_string<TCHAR> pc(infoBuf);Prasad Notifier using ATL | Operator new[],delete[][^]
-
Sorry but I have another question ....:sigh: How do I now convert wstring to string, Cause see I have this function that accepts a std:string and not a std::wstring ... :confused: ERROR: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::wstring' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 24 Thanx in advance (again)
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
Not pretty but it works :wtf::omg:
wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf; char b[INFO_BUFFER_SIZE]; string a; wcstombs(b,infoBuf,INFO_BUFFER_SIZE); strcpy((char*)a.c_str(),(char*)b);
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
One more way to achieve this. Save one function call,too.
TCHAR infoBuf[INFO_BUFFER_SIZE];
GetComputerName( infoBuf, &bufCharCount );
basic_string<TCHAR> pc(infoBuf);Prasad Notifier using ATL | Operator new[],delete[][^]
Thnx Prasad, But my fubction accepts a std::string and when I do this:
TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); basic_string pc(infoBuf);
this would happen: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 32The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Not pretty but it works :wtf::omg:
wchar_t infoBuf[INFO_BUFFER_SIZE]; DWORD bufCharCount = INFO_BUFFER_SIZE; GetComputerName( infoBuf, &bufCharCount ); wstring pc = infoBuf; char b[INFO_BUFFER_SIZE]; string a; wcstombs(b,infoBuf,INFO_BUFFER_SIZE); strcpy((char*)a.c_str(),(char*)b);
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
I would suggest some modifications to your code,
TCHAR infoBuf\[INFO\_BUFFER\_SIZE\]; DWORD bufCharCount = INFO\_BUFFER\_SIZE; GetComputerName( infoBuf, &bufCharCount ); string a;
#ifdef UNICODE
size_t i;
char temp[INFO_BUFFER_SIZE];
wcstombs_s(&i,temp,(size_t)INFO_BUFFER_SIZE,infoBuf,(size_t)INFO_BUFFER_SIZE);
a.assign(temp);
#else
a.assing(infoBuf);
#endifPrasad Notifier using ATL | Operator new[],delete[][^]
-
I would suggest some modifications to your code,
TCHAR infoBuf\[INFO\_BUFFER\_SIZE\]; DWORD bufCharCount = INFO\_BUFFER\_SIZE; GetComputerName( infoBuf, &bufCharCount ); string a;
#ifdef UNICODE
size_t i;
char temp[INFO_BUFFER_SIZE];
wcstombs_s(&i,temp,(size_t)INFO_BUFFER_SIZE,infoBuf,(size_t)INFO_BUFFER_SIZE);
a.assign(temp);
#else
a.assing(infoBuf);
#endifPrasad Notifier using ATL | Operator new[],delete[][^]
Thnx for the pointers Prasad ... I appriciate the help :)
The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
-
Thnx Prasad, But my fubction accepts a std::string and when I do this:
TCHAR infoBuf[INFO_BUFFER_SIZE]; GetComputerName( infoBuf, &bufCharCount ); basic_string pc(infoBuf);
this would happen: Error 1 error C2664: 'CIniFile::SetValue' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'std::string' c:\documents and settings\qx55246\my documents\visual studio 2005\projects\testsmsintferface\testsmsintferface\testsmsintferface.cpp 32The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r
If your language/tool supports WideCharToMultiByte and MultiByteToWideChar functions, you may use them for ASCII<->UNICODE string conversion. Hope this helps. Anand