Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Wide String conversions

    SDK Help
    0
    3
    575
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Helper
      last edited by

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

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

      ---------
      Hello,
       
      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.

      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

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

        Hi,

        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.

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post