Convert Matlab string to wchar in C-Mex under Windows and Linux
7 ビュー (過去 30 日間)
古いコメントを表示
I want to convert a Matlab string to a wchar string, such that I can use e.g. either the C-function _wopen? Or to a LPCWSTR under Windows?
Matlab's CHAR arrays use 2 bytes per element. Inside a C-Mex function I can use mxArrayToString to convert a Matlab CHAR array to a C char * vector. The documentation of mxArrayToString state, that "it supports multibyte character sets". Unfortunately this is not explained with any further details.
Currently under Windows I copy the contents of the mxChar array to a wchar_t vector and append a L'\0'. It seems to work, but I'm not really convinced that this is stable. Under Linux a wchar has a length of 4 bytes and the direct copy is not sufficient.
Are Matlab CHARs UTF16 encoded? How does mxArrayToString support multibyte charcter sets exactly? How do I convert mxChar to wchar correctly and efficiently under Windows and Linux?
2 件のコメント
Kaustubha Govind
2011 年 3 月 16 日
Jan: If you don't receive a reply here, Tech Support might be able to give you an answer.
回答 (1 件)
Alexander H
2019 年 1 月 25 日
Jan, thanks for your post.
Recently, I got entangled in passing Chinese word (non-latin character) from Matlab to mex. I tried wchar_t, mxChar, none of them works. While, mxArrayToString you mentioned works exactly as I want. Since I just want my mex function can deal with Chinese file path , so I didn't copy it to somewhere else. The code is simply as:
char *filename = mxArrayToString(prhs[0]);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!