UTF-8 strings in MEX-files
Show older comments
This question has been asked here before, but not with any satisfying answers. Since all those answers, a new documented function mxArrayToUTF8String has appeared. I'm hoping to find the function that does the reverse: make an mxArray from a UTF-8 encoded C or C++ string. I'm OK with an undocumented function, or using a bit of code from someone else. I'm not OK with linking some huge Unicode library, which I have no use for. All I need is convert UTF-8 to UTF-16 (which seems to be what MATLAB uses in their mxChar arrays).
Does anybody have any experience with UTF-8 encoded strings in MATLAB?
What does The MathWorks suggest we do if we want to work with UTF-8 encoded strings?
Accepted Answer
More Answers (2)
Walter Roberson
on 26 Mar 2017
1 vote
I am not all that familiar with those APIs, but how about if you mxCreateString to copy the char* into an mxArray, and then call into MATLAB to execute native2unicode() with 'UTF8' as the encoding?
1 Comment
Cris Luengo
on 26 Mar 2017
Not a solution, but a contribution to the discussion:
See this discussion: http://www.mathworks.com/matlabcentral/newsreader/view_thread/301249 . The most reliable method I've found is http://site.icu-project.org/, which exactly matchs your excluded "*not* OK with linking some huge Unicode library".
Matlab and most libraries for C or the OS-APIs have a stable Unicode support. It is a pitty, that many conversion functions of the MEX API are not documented.
2 Comments
Cris Luengo
on 26 Mar 2017
Bart Plovie
on 24 Jul 2023
Had to do this today as well, and was a bit surprised there was no built-in function. I suspect UTF-16 is used since that's what Windows uses. So given the percentage of MATLAB users on Windows, I think it's a fair assumption on their part.
Anyhow, if someone else ever runs into this issue: on Windows you can solve it using MultiByteToWideChar (just include windows.h) like this:
int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, NULL, 0);
wchar_t* utf16String = (wchar_t*) mxMalloc(bufferSize * sizeof(wchar_t));
int rc = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, utf16String, bufferSize);
On Linux you can use iconv if you need something similar done:
iconv_t conversionDescriptor = iconv_open("UTF-16LE", "UTF-8");
int utf8Size = strlen(utf8String);
int bufferSize = utf8Size * sizeof(wchar_t);
wchar_t* utf16String = (wchar_t*) mxMalloc(bufferSize);
char* inbuf = (char*)utf8String;
char* outbuf = (char*)utf16String;
int rc = iconv(conversionDescriptor, &inbuf, &utf8Size, &outbuf, &bufferSize);
You might need to modify these a bit, but I hope it gets the idea across. I'd imagine UTF-16 little endian is used on Linux, but if it isn't you can just drop the LE.
Also, if you want to grab stuff from MATLAB with mxGetString() you can convert it to pretty much any format you want by first converting it to UTF-16 (WideChar) using CP_ACP as source encoding and MultiByteToWideChar, and then convert it back to MultiByte using the desired format. It's a bit of a hacky work-around, but windows's encoding translation seems to be pretty solid, just be careful with the buffer size allocation.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!