Wide String conversions

  • On 22/11/2017 at 06:29, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R19 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    I'm trying to get a wide string out of the C4D String class, but I'm getting garbage at the end of it.

     // const Filename &name  
     // contains "C:\myfile.txt"  
     String str = name.GetFileString();  
     maxon::BaseArray<Utf16Char> arr;  
     if(str.GetUtf16(arr)) {  
      const wchar_t *ptr = (const wchar_t * )arr.GetFirst();  
      wprintf(L"ptr: %s\n", ptr );  
    It prints:  
    C:\myfile.txt   <garbage chars at end>  

    What am I doing wrong?  Is it bad to assume that GetUtf16 arrays are always terminated by a NULL?

    I also would like to convert a wide string to String:

    wchar_t wfilename[256] = L"C:\\myfile.txt";  
    String str;  
    str.SetUtf16( (const Utf16Char * )wfilename, -1 );  

    Is this correct as well?

    Using C4D C++ SDK.

  • On 23/11/2017 at 03:18, xxxxxxxx wrote:


    As stated in the docs, the buffer returned by GetUtf16() is not null terminated.
    So you have to add it to the code with arr.Append(0) before conversion to wchar_t*.
    The code for the other way round (from wchar_t* to String) looks fine.

  • On 23/11/2017 at 12:32, xxxxxxxx wrote:

    Thanks for the reply.

    I didn't know about arr.Append(0).  I found another way using Count(), but your method is much cleaner.

Log in to reply