为什么char在Matlab S-Function中存储为2个字节

Why char is stored as 2 bytes in Matlab S-Function

我想将一个 uint8 数组作为参数传递给我的 S-Function:

inParam = char(uint8(1:7))

在 S-Function 中我做了以下操作

UINT8_T *inParam = (UINT8_T *)mxGetPr(ssGetSFcnParam(S, PARAM_IN_PORT_NR)); //;

但我看到实际上数组元素存储为 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++);

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

MATLAB 对字符向量和字符串使用 UTF-16 编码。例如,将 definition of matlab::engine::String 视为 std::basic_string<char16_t>。这在所有平台上都是一样的。我不知道 MATLAB 何时引入了 Unicode 支持,但您可以假设过去 15 年的任何版本都使用 16 位字符编码。

如果您需要支持 Unicode 字符,请考虑使用 mxGetString to get an 8-bit (ASCII) representation of the string, or mxArrayToString