ein*_*123 5 string matlab type-conversion matlab-coder
我正在使用Matlab Coder将一些Matlab代码转换为C++,但是我无法将整数转换为字符串.
int2str()代码生成不支持,所以我必须找到一些其他方法将int转换为字符串.我试过谷歌搜索,没有成功.这甚至可能吗?
这可以手动完成(但非常痛苦)
function s = thePainfulInt2Str( n )
s = '';
is_pos = n > 0; % //save sign
n = abs(n); %// work with positive
while n > 0
c = mod( n, 10 ); % get current character
s = [uint8(c+'0'),s]; %// add the character
n = ( n - c ) / 10; %// "chop" it off and continue
end
if ~is_pos
s = ['-',s]; %// add the sign
end
Run Code Online (Sandbox Code Playgroud)
sprintf 是另一个非常基本的功能,所以它也可以在C++中工作:
x = int64(1948)
str = sprintf('%i',x)
Run Code Online (Sandbox Code Playgroud)
它也是使用的基础功能int2str.
根据Matt在评论中指出的这一全面的支持功能列表,sprintf不支持,这是令人惊讶的.然而,有一个未记录的辅助函数(因此不在列表中)sprintfc似乎有效,可以等效使用:
str = sprintfc('%i',x)
Run Code Online (Sandbox Code Playgroud)