cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4, Could I extend a string with sensor value?

tanghotin2004
Associate
Posted on March 25, 2015 at 07:03

Thank for your look and help.

I want to send a string ''&sharp1250♯'', and the 1250 is the sensor value.

So I do some try.

            uint16_t ch=1250;

          uint16_t Value[4];

          char str[10] = ''♯'';

            Value[0] = (ch /1000 )+48 ; // divide number by 1000

            ch = (ch % 1000) ; // take remainder number of 1000

            Value[1] = (ch / 100 )+48; // divide this now by 100

            ch = (ch % 100 )  ;// take remainder of 100

            Value[2] = (ch / 10)+48 ;  // divide remainder by 10

            Value[3] = (ch % 10 ) +48; // load remainder of final Dimension into unit variable

          strcat(str,Value);

          strcat(str,''♯'');

However, when I look at debug mode, it give me the result

str[0]='♯'

str[1]='1'

str[2]='2'

str[3]='5'

str[4]='0'

str[5]=2

str[6]='♯'

there is a non need 2 appear in str[5]

but when I change  strcat(str,Value) to  strcat(str,''1250'')

there are no matter.

May I ask what is the problem of doing this?

#string
3 REPLIES 3
ivani
Associate II
Posted on March 25, 2015 at 19:02

There is nothing related with STM32 chips.

The issue is that strcat() operates with NULL terminated strings (i.e. arrays of chars). This means that Value should be defined as:

char  Value[5];

And you should add:

Value[4] = 0;

francescatodiego
Associate II
Posted on March 25, 2015 at 19:52

But the simplest way to do this is

sprintf (str, ''#%04d#'', ch) ;

with

all

printf formatting options

you

can experiment

online

with

the

C compiler

that you find

here http://www.tutorialspoint.com/compile_c_online.php
tanghotin2004
Associate
Posted on March 26, 2015 at 07:49

Thank you, it is working now