由于某种原因,我在将字符串从 SO 复制粘贴到 MATLAB 时遇到困难,因为“è”显示为 char(65533)(而不是正确的 char(232))...
无论如何,我拼凑了一个小型转换实用程序,将字符串或单元字符串转换为它们的 Unicode-in-HTML 等效项,以补充 horchler 的回答:
function html = toHTML(strings)
%% Initialize
% Basic IO check
if ~iscellstr(strings) && ~ischar(strings)
error(...
'toHTML:invalid_input',...
['Invalid input class: ''%s''.\n',...
'Supported input types are ''char'' or a ''cell'' containing ''char''.'], class(strings));
end
% Provide support for
% - Single and multiline line char arrays
% - Cellstrings
wasChar = ischar(strings);
if wasChar
if size(strings,1) > 1
strings(:, end+1) = char(10);
end
strings = {strings};
end
%% Convert all strings to their unicode representation in HTML
% Just for abbreviation
uf = {'UniformOutput',false};
% Convert all characters to their HTML unicode representation
html = cellfun(@transpose, strings, uf{:});
html = cellfun(@(x) cellstr(num2str(x(:)+0)), html, uf{:});
html = cellfun(@(x) cellfun(@(y) ['&#' strtrim(y) ';'],x, uf{:}), html, uf{:});
% Include HTML tags
html = cellfun(@(x) ['<html>' [x{:}] '</html>'], html, uf{:});
% Take care of newlining
html = regexprep(html, ' ', '<br>');
html = regexprep(html, '<br></html>$', '</html>');
% Make output type consistent with input type
if wasChar
html = [html{:}];
end
end
我目前也将其提交给 FEX。如果有人知道这样的事情是否已经存在,请告诉我。