cancel
Showing results for 
Search instead for 
Did you mean: 

Int2str function improvement

spiovan9
Associate II
Posted on June 15, 2010 at 17:25

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 #int2str
7 REPLIES 7
Grom
Associate II
Posted on July 07, 2010 at 14:32

Hello Stefano,

Thanks for this feedback, I will correct this point in the code sources.

Rgds

Grom
vlad_kn2000
Associate II
Posted on July 09, 2010 at 06:16

Workalot
Associate III
Posted on July 16, 2010 at 04:16

Workalot
Associate III
Posted on July 19, 2010 at 00:53

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

}

-------------------------

RomainR.
ST Employee
Posted on July 23, 2010 at 15:16

Hi,

Attached you will found my own string_conversion library. It very usefull when you want use lcd display or uart with terminal interfacing.

Functions can convert signed/unsigned char, int, double and float into string. And unsigned char into hex and bin format. 

I hope you enjoy it...

Zip file (STVD + Raisonance Compiler):

string_conversion.h

string_conversion.c

main.c

(Note: you should include string.h for strcpy function)

Regards,

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.

spiovan9
Associate II
Posted on January 23, 2011 at 19:29

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;

}

RomainR.
ST Employee
Posted on January 25, 2011 at 10:34

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.stefano

Posted: Sunday, January 23, 2011 7:29 PM

Subject: Int2str function improvement

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;

}

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.