为什么char在Matlab S函数中存储为2个字节

OHL*_*ÁLÁ 2 matlab s-function

我想将uint8数组作为参数传递给我的S函数:

inParam = char(uint8(1:7))
Run Code Online (Sandbox Code Playgroud)

在S功能中,我做了以下工作

UINT8_T *inParam = (UINT8_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //;
Run Code Online (Sandbox Code Playgroud)

但是我看到实际上数组元素存储为2个字节。

UINT16_T *inPorts = (UINT16_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //
// I can loop through the data
// This is only a snippet
*(inPorts++);
Run Code Online (Sandbox Code Playgroud)

这是为什么?在所有的Matlab版本中都会发生这种情况吗?

Cri*_*ngo 6

MATLAB将UTF-16编码用于字符向量和字符串。例如,看到MATLAB ::引擎的定义::字符串作为std::basic_string<char16_t>。在所有平台上都是一样的。我不知道MATLAB中何时引入了完全Unicode支持,但是您可以假定过去15年中的任何版本都使用16位字符编码。

考虑使用mxGetString来获取字符串的8位(ASCII)表示形式,或者mxArrayToString如果您需要支持Unicode字符。