STM32F4, Could I extend a string with sensor value?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2015-03-24 11:03 PM
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
This discussion is locked. Please start a new topic to ask your question.
3 REPLIES 3
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2015-03-25 11:02 AM
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;
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2015-03-25 11:52 AM
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.phpOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2015-03-25 11:49 PM
Posted on March 26, 2015 at 07:49 Thank you, it is working now
