This is a two step process. The first phase is performed in C and the second phase in MATLAB (called from C).
(1) In the C code, you will need to create an mxArray of UINT8 from the UTF-8 C-string. If you intend to have multiple rows of characters in your array, keep in mind that MATLAB stores arrays in column-major order.
(2) In MATLAB you will need to use the 'native2unicode' function to convert the array of 'uint8' to an array of 'char'.
For example, if this conversion is taking place in a MEX file, the code would look something like this: (NOTE: This code does not compile as-is)
//UTF-8 string to convert
char utf8str[] = {65 66 67 68 207 163};
// Step 1 - create a mwArray of uint8 from UTF-8 encoded C-string
// **YOUR FUNCTION** Convert C-string to mwArray of uint8
mwArray* uint8Array = UTF8CStringToMwArrayUint8(utf8str, length);
//Step 2 - convert mwArray of uint8 to mwArray of char using 'native2unicode'
mwArray* encodingString = mxCreateString("UTF-8");
mwArray* lhs_native2unicode[1];
mwArray* rhs_native2unicode[] = {uint8Array, encodingString};
// Calling 'native2unicode'
mexCallMATLAB(1, &lhs_native2unicode, 2, &rhs_native2unicode, 'native2unicode');
// this is the converted char array
mwArray* charArray = lhs_native2unicode[1];
Best Answer