?
Solved

Converting CString to *char

Posted on 2006-05-27
7
Medium Priority
?
364 Views
Last Modified: 2012-06-27
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
Comment
Question by:killaz4life
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 16776401
you mean like this ..?

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

Expert Comment

by:DrAske
ID: 16776410
or

char* data_char = data.GetBuffer();
//When you're done using data_char, use ReleaseBuffer().
data.ReleaseBuffer();
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 16776556
>> 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Expert Comment

by:leroopsin
ID: 16781650
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
 

Assisted Solution

by:leroopsin
leroopsin earned 100 total points
ID: 16781657
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
 

Author Comment

by:killaz4life
ID: 16798560
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 16799689
Glad to help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question