Link to home
Start Free TrialLog in
Avatar of camster123
camster123

asked on

How to convert utf32 to utf16 using C++ on Ubuntu Linux 15.10 with the gcc c++11 compiler.

I read this link, stackoverflow.com/questions/23919515/how-to-convert-from-utf-16-to-utf-32-on-linux-with-std-library,
on how to convert utf16 to utf32 using C++ on Ubuntu Linux 15.10 with the gcc c++11 compiler.
   I would like to find out how to convert utf32 to utf16 using C++ on Ubuntu Linc 15.10 with the gcc c++11 compiler.
   This is not a homework assignment question.
    Thank you.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of camster123
camster123

ASKER

@sarabande,  Thank you for your excellent solution.

How should I marshal an array of unmanaged C++

struct CC_STR32
{
    wchar_t szString[32] ;
}

to an managed C# array of IntPtr's on Ubuntu Linux 15.10 and Mono version 4.2.1?
i don't know much about c# on linux. at Windows IntPtr is defined for native c++ and c# and has the same size. you may use it for a native pointer - void* - which is guaranteed to fit into IntPtr. for win32 platform IntPtr is 32-bit and size 4. instead of pointers you could marshal 32-bit integers or even smaller integers like wchar_t.

if you are lucky, the same applies for your platform. find out, whether the gcc has type IntPtr or intptr_t and whether it is 32 bit or 64 bit. also find out which size wchar_t is. do the same for c# on Linux.

if sizes match you may provide a pointer to an array of IntPtr from c++ and have the same at the c#. if wchar_t type is 16-bit on both you have the alternative to use an array of IntPtr's as 'transport' unit - or - transfer it as BYTE array (pointer to unsigned char) instead. most likely you have a 3rd alternative where there is a marshalling function for wchar_t.

if wchar_t is 32-bit (and utf-32) on gcc you could use a pointer to that buffer if IntPtr at c# is also 32 bit. then at c# you have to build the string from IntPtr array (or using the right marshalling function).

actually, there are a lot of possibilities but the solution finally will end into something like the while loop i posted in the previous comment respectively into a function call which takes an IntPtr array and returns a string.

Sara
@Sarabande's solution is excellent and very useful as always.