AnsweredAssumed Answered

Int2str function improvement

Question asked by piovan.stefano on Jun 15, 2010
Latest reply on Jan 25, 2011 by romain reicher
Dear friends,

this improved version of the "int2str" function will solve a bug present in the original version provided with the hyperterminal example though it is used nowhere in that example.

I'm working on an application to perform analog conversion and then send the value to the hyperterminal like a simple datalogger. When converting the int data from the adc to a string the original routine doesn't manage the data length changing (i.e. when the data turn from 1024 to 0 the string losses the string termination '\0').

Hope it helps
Stefano


/*******************************************************************************

* Function Name : Int2Str

* Description : Convert an Integer to a string

* Input : - str: The string

* - intnum: The intger to be converted

* Output : None

* Return : None

*******************************************************************************/

void Int2Str(char *str, u32 intnum)

{

u32 Div = 1000000000;

int i, j = 0, Status = 0;

for (i = 0; i < 10; i++)

{

str[j++] = (intnum / Div) + 48;

intnum = intnum % Div;

Div /= 10;

 

if ((str[j-1] == '0') & (Status == 0))

{

str[j] = '\0';

j = 0;

}

else

{

str[j] = '\0';

Status++;

}

 

}

}

Outcomes