HowTo:Convert wchar t to SPAXMILUnicodeChar which Generic APIs expect?
From DocR21
If you are working with the 3D InterOp Generic translators on UNIX and have embraced its Unicode support, then soon you may stumble upon the above question.
The Generic Unicode string is UTF-16 and there is no direct conversion of wchar_t to UTF-16 on UNIX.
Important: You will need to have an in-house Unicode infrastructure (which is less preferred), or need to have third party tools such as ICU (which is much preferred) in order to support Unicode in your application.
The following code snippet assumes that you are using the ICU library and you are conversant with its APIs.
Example
The conversion takes place like this: wchar_t -> UTF16-> SPAXMILUnicodeChar
// The name of the class used from ICU is UnicodeString // Stage 1: Construct UnicodeString object from wchar_t UnicodeString * pNewString = NULL; UChar * uCharBuffer = NULL; wchar * pwszStr = "My wide char string"; // Find the length of pwszStr. This can be computed by incrementing the pointer to string and counting until we hit '\0'. // Let us say the length is 'len' int32_t len = getStringLength( pwszStr ); // assuming you have the implementation of 'getStringLength' UErrorCode err( U_ZERO_ERROR ); int32_t bufferlen = 0; u_strFromWCS( NULL, 0, &bufferlen, pwszStr, len, &err ); // Note that anything starting with 'u' is an ICU function. if( U_ZERO_ERROR == err || U_BUFFER_OVERFLOW_ERROR == err || U_STRING_NOT_TERMINATED_WARNING == err ) { uCharBuffer = (UChar *) uprv_malloc( sizeof(UChar) * ( bufferlen + 1) ); err = U_ZERO_ERROR; int32_t charsConverted = 0; u_strFromWCS( uCharBuffer, bufferlen + 1, &charsConverted, pwszStr, len, &err ); } else { Badconversion = true; // Return gracefully from here. } pNewString = new UnicodeString( uCharBuffer ); // We have UnicodeString from wchar_t uprv_free( uCharBuffer ); // Stage 2: Convert UnicodeString to UTF-16 int32_t len = pNewString->length(); UChar * pBuffer = (UChar *)uprv_malloc( sizeof(UChar) * (len+1) ); pNewString->extract(0, len, pBuffer); // Now we have UTF-16 string. // Stage 3: Cast the string from stage-2 directly into SPAXMILUnicodeChar * [only for UNIX and Generic combination] SPAXMILFileReadOpt readOptions; int partcount = 0; SPAXMILParts * parts = NULL; SPAXMILRead( (SPAXMILUnicodeChar *)pBuffer, &readOptions, &partcount, &parts);
See Also
- See also: Category:FAQs
