Convert int to string without using int2str()

3 ビュー (過去 30 日間)
Einar Sandberg
Einar Sandberg 2015 年 7 月 23 日
コメント済み: Ryan Livingston 2015 年 7 月 24 日
I'm using Matlab Coder to convert some Matlab code to C++, however I'm having trouble converting int to strings.
int2str() is not supported for code generation, so I must find some other way to convert ints to strings. I've tried googling it, without success. Is this even possible?
  1 件のコメント
Ryan Livingston
Ryan Livingston 2015 年 7 月 24 日
For the benefit of future readers, see http://stackoverflow.com/q/31581266/3297440 for more discusson

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 7 月 23 日
Of course, it's possible. How do you write a number? You write down each decimal digit one by one. Just do the same: Determine each decimal digit of your integer and convert it to a character. A very crude way of doing it is:
%only work for unsigned integer. it's trivial to test for sign beforehand and work on the absolute value
str = '';
while number > 0
digit = mod(number, 10);
str = [digit + '0', str]; %digit + '0' convert an integer between 0-9 into the corresponding character
number = floor(number / 10);
end
  1 件のコメント
Einar Sandberg
Einar Sandberg 2015 年 7 月 23 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by