Main Content

unicode2native

Unicode 字符表示形式转换为数值字节

语法

bytes = unicode2native(unicodestr)
bytes = unicode2native(unicodestr,encoding)

说明

bytes = unicode2native(unicodestr) 将 Unicode® 字符表示形式的输入 unicodestr 转换为用户默认编码,并将字节作为 uint8 向量 bytes 返回。输出向量 bytes 具有与 unicodestr 输入相同的常规数组形状。您可以使用 fwrite 函数将 unicode2native 的输出保存到文件中。unicodestr 可以是字符向量或字符串标量。

bytes = unicode2native(unicodestr,encoding)unicodestr 转换为由 encoding 指定的字符编码方案。输入参数 encoding 或为空 (''),或为编码方案的名称或别名。下面是一些示例:'UTF-8''latin1''US-ASCII''Shift_JIS'。如果 encoding 未指定或不包含任何字符 (''),则使用默认编码方案。encoding 可以是字符向量或字符串标量。

示例

以下示例以包含 Unicode 字符表示形式的两个字符向量开始。假定 str1 包含西欧语言文字,str2 包含日语文字。该示例将两个字符向量写入同一个文件中,对第一个字符向量使用 ISO-8859-1 字符编码方案,对第二个字符向量使用 Shift-JIS 编码方案。该示例使用 unicode2nativestr1str2 转换为相应编码方案。

fid = fopen('mixed.txt', 'w');
bytes1 = unicode2native(str1, 'ISO-8859-1');
fwrite(fid, bytes1, 'uint8');
bytes2 = unicode2native(str2, 'Shift_JIS');
fwrite(fid, bytes2, 'uint8');
fclose(fid);

扩展功能

版本历史记录

在 R2006a 之前推出

另请参阅