2010-06-15 08:25 AM
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++;
}
}
}
#string #stm8s-discovery #int2str #int2str2010-07-07 05:32 AM
2010-07-08 09:16 PM
2010-07-15 07:16 PM
2010-07-18 03:53 PM
Correction if you please
-------------------------
int Int2Str(char *str, u32 intnum)
{ u32 Div = 1000000000L;
char* p = str;
while ( intnum < Div ) // Skip unsignificant zeros
{ Div /= 10; //
}
while ( Div ) // Convert the number to string { // *p++ = intnum/Div + '0'; // save a digit intnum %= Div; // Div /= 10; // }
*p = 0; // Append trailing null
return ( p - str );
// Return length of string
}-------------------------
2010-07-23 06:16 AM
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2011-01-23 10:29 AM
Hi Romain,
thankyou very much for the string functions you posted. I have just a question: what is the purpose of the SC_BUFFER_OFS_PTR = 5 constant? I traced down, for example, the following function ''sc_format'' and found that if you pass to the function u8 digits = 16 (as in a LCD display) or any other value which is > 5, the *--pstr = SC_FILL_CHAR; goes outside the memory allocation of the buffer string. I.E. it goes under the [0] position of the string buffer. Thank you Stefano /* Function Definition -------------------------------------------------------*/ // Do formatted output u8 *sc_format(u8 *pstr,u8 digits){ u8 len = (u8)(pstr - SC_BUFFER_OFS_PTR); // Get length of string while (digits--) // Sting loop { if (len) // If character created by conversion, just skip left in buffer { pstr--; len--; } else // It must be a fill character { *--pstr = SC_FILL_CHAR; } } return pstr; }2011-01-25 01:34 AM
Hi Stefano,
So
I wrote
this
code
a few years ago
now
,
I
'll
try
to
answer
your questions
:
sc_buffer_ofs_ptr is adresse of the string + decimal point number offset.u8
variable
digit
in
all
functions
is the
number of
characters after
the
decimal
point
.
At the
time
I
wanted
to limit
that number
without
actually
providing
security
easily
portable
on a
simple
8-bit
microcontroller
.
It is
true
that
as
the function
is written
,
if
u8
digit
>
5
it
goes outside
of
the allocation of
the
buffer size
.
This
code
works
fine,
but
it
is
not perfect
.
I
suggest you
limit the
number
of
decimal
digits
u8
= 3. So, 4 work too!
Regards, romain From: piovan.stefanoPosted: Sunday, January 23, 2011 7:29 PMSubject: Int2str function improvementHi Romain,
thankyou very much for the string functions you posted. I have just a question: what is the purpose of the SC_BUFFER_OFS_PTR = 5 constant? I traced down, for example, the following function ''sc_format'' and found that if you pass to the function u8 digits = 16 (as in a LCD display) or any other value which is > 5, the *--pstr = SC_FILL_CHAR; goes outside the memory allocation of the buffer string. I.E. it goes under the [0] position of the string buffer. Thank you Stefano /* Function Definition -------------------------------------------------------*/ // Do formatted output u8 *sc_format(u8 *pstr,u8 digits){ u8 len = (u8)(pstr - SC_BUFFER_OFS_PTR); // Get length of string while (digits--) // Sting loop { if (len) // If character created by conversion, just skip left in buffer { pstr--; len--; } else // It must be a fill character { *--pstr = SC_FILL_CHAR; } } return pstr; }To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.