stringmatlabtype-conversionmatlab-coder

Convert integer to string with C++ compatible function for Matlab Coder


I'm using Matlab Coder to convert some Matlab code to C++, however I'm having trouble converting intergers 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?


Solution

  • This can be done manually (very painful, though)

    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