• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Converting CString to *char

I have a small problem, when im loading the bitmap file in MFC, apparently it has to be in CString. I added some files and in order for me to process that loaded image, I need to convert that CString to *char. If there is any process I can do to do this.
0
killaz4life
Asked:
killaz4life
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
DrAskeCommented:
you mean like this ..?

CString data="somestring";
char * data_char= new char[data.GetLength()+1];
strcpy(data_char, data);
0
 
DrAskeCommented:
or

char* data_char = data.GetBuffer();
//When you're done using data_char, use ReleaseBuffer().
data.ReleaseBuffer();
0
 
mrwad99Commented:
>> CString data="somestring";
char * data_char= new char[data.GetLength()+1];
strcpy(data_char, data);

This will not work.  You are trying to place a CString into a char* with your third line.

The correct way is to do something like this:

CString strString = _T("My String");
const TCHAR* pString = (LPCTSTR)strString;

where TCHAR is either a char or wchar_t depending on whether you have UNICODE defined.  If you always want a char*, use

char* pString = (char*)(LPCTSTR)strString;

There is an excellent article on CString management at http://www.flounder.com/cstring.htm

Of special interest is

tp://www.flounder.com/cstring.htm#CString%20to%20char%20*%20I:%20Casting%20to%20LPCTSTR
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
leroopsinCommented:
Dear All,

There are two cases for the solution
1. Unicode
2. MSBC
---------------------------------------------------------
I support a method to convert UnicdeToAscii as following:

char* CharsetConversion::UnicodeToAscii(const wchar_t * p_szUnicode)
{
    int _iLength = wcslen(p_szUnicode) + 1;
    char* _szAsciiRet = new char[_iLength];
    char* _pszAsciiRet = _szAsciiRet;
    for(int i=0; i<_iLength; i++)
    {
        *_pszAsciiRet++ = static_cast<char>(*p_szUnicode++);
    }

    return _szAsciiRet;
}

I have a CString as following:

CString szObjSpect = _T("abc123");

You can use this code to slove:

#ifdef _UNICODE
        char* _buffer = CharsetConversion::UnicodeToAscii(szObjSpect.GetBuffer(0));
        strcpy(_pData0,_buffer);
        delete _buffer;
#else
            strcpy(_pData0,szObjSpect);
#endif

I think it is helpful for you.

Sin Le
0
 
leroopsinCommented:
Dear All,

There are two cases for the solution
1. Unicode
2. MSBC
---------------------------------------------------------
I support a method to convert UnicdeToAscii as following:

char* CharsetConversion::UnicodeToAscii(const wchar_t * p_szUnicode)
{
    int _iLength = wcslen(p_szUnicode) + 1;
    char* _szAsciiRet = new char[_iLength];
    char* _pszAsciiRet = _szAsciiRet;
    for(int i=0; i<_iLength; i++)
    {
        *_pszAsciiRet++ = static_cast<char>(*p_szUnicode++);
    }

    return _szAsciiRet;
}

I have a CString as following:

CString szObjSpect = _T("abc123");

You can use this code to slove:
-------------------------------
char* _pData0 = _pItem0->getBuffer(iLength+1);
#ifdef _UNICODE
        char* _buffer = CharsetConversion::UnicodeToAscii(szObjSpect.GetBuffer(0));
        strcpy(_pData0,_buffer);
        delete _buffer;
#else
          strcpy(_pData0,szObjSpect);
#endif
-------------------------------

I think it is helpful for you.

Sin Le
0
 
killaz4lifeAuthor Commented:
Very interesting codes from all, but mrwad99 has the correct answer 100% very simple and easy. I havent tried your code Sin Le since it seems rather complicated but it is probably certain it work in different situations.
thanks
0
 
mrwad99Commented:
Glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now