[HC08 - CW6] How to append a uint8_t to a string

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

[HC08 - CW6] How to append a uint8_t to a string

14,510 Views
CecchiSandrone
Contributor II
Hi all.
I need to build a string appending a uint8_t. I tried with:

Code:
void sendLQI(uint8_t linkQuality) {   char msg[30] = "Link quality: ";   (void)strcat(msg,(char *)linkQuality);   (void)Uart2_Transmit(msg,30,NULL);}

 But it doesn't seem to work. Any suggestion?

Labels (1)
0 Kudos
8 Replies

2,460 Views
Technoman64
Contributor III
I use the following functions to build text string with integer. Use Digits to set number of digits for number and decimal to set number of decimal places in converted number. Use decimal = 0 to leave decimal point out.
 
Lets say you use an integer to store the value 1000 and you want to represent as "10.00" in text string. You would then use Digits = 5 and Decimal = 2 when calling the function.
 
 
/* Call function like this */
char Msg[30];
ItoaMsg(&Msg[0], "Link Qaulity = ", 30, 2, 0);
 
Msg will contain the text message "Link Quality = 30" and will be NULL terminated ready to use.
 
Hope this helps
 
 
Code:
/* Integer to ASCII */void Itoa(int Number, char Digits, char Decimal, char *String){ /* Get starting position in string array */ char *PositionSave, *Position = String + Digits;  /* Negative flag */ char NegativeFlag = FALSE; /* Charactor buffer */ char Chr; /* Working UINT16 */ unsigned int uInteger; /* Loop index */ int Index; /* Number of digits converted */ int DigitCount = 0;  /* Make sure we are converting more than one digit */ if(!Digits)  return;  /* Are we working with a negative number— */ if (Number < 0) {  /* It is a negative number */  NegativeFlag = TRUE;  /* Remove the sign bit from the integer */  uInteger = ((unsigned int)(-(1 + Number))) + 1; } else {  /* Not a negative number */  uInteger = Number; }  /* NULL terminate the string */ *Position = 0;  /* Do the conversion */ do {  /* Decrement the digits counter */  Digits--;  /* Get the ASCII charactor */  *--Position = '0' + (uInteger % 10);  /* Divide by 10 */  uInteger /= 10;  /* Have we reached the end of the number– */  if(!uInteger)   break;  /* Increment the digit counter */  DigitCount++;   } while (Digits);  /* Do we need to add a decimal point˜ */ if(Decimal && Digits) {  /* Remove another digit position */  Digits--;    /* Do we have enough space for a leading 0™ */  if(Digits && (DigitCount < Decimal)){   for(Index = Decimal - DigitCount; Index > 0; Index--) {    *--Position = '0';    DigitCount++;    Digits--;    if(!Digits)     break;   }  }    /* Decrement and save the pointer */  Position--;  PositionSave = Position;      /* Move the charactors */  for(Index = DigitCount - Decimal; Index >= 0; Index--) {   Chr = Position[1];   Position[0] = Chr;   Position++;  }  /* Insert the decimal point */  *Position = '.';  /* Restore the pointer */  Position = PositionSave; }    /* Do we need to add the negative sign? */ if (NegativeFlag && Digits) {  /* Add the negative charator */  *--Position = '-';  /* Reduce the digit count */  Digits--; }  /* Do we need to pad any charactor positions? */ for(Index = Digits; Index != 0; Index--)   *--Position = ' ';}/* ItoaMsg */void ItoaMsg(char *Buffer, char *Msg, int Number, char Digits, char Decimal) { /* Local variables */ char Array[11];  /* Convert Number to string */ Itoa(Number, Digits, Decimal, &Array[0]);  /* Copy Msg to Buffer */ (void) strcpy(Buffer, Msg);  /* Add Number string to Buffer */ (void) strcat(Buffer, &Array[0]);}

 


Message Edited by Technoman64 on 2007-11-14 02:20 AM
0 Kudos

2,461 Views
CecchiSandrone
Contributor II

 

Thanks for help Technoman64 I will try your function soon. However, I don't understand why in the debugger I can see 2 rows relative to the variable linkQuality as you can see in the image. In fact I took linkQuality directly from the original structure and its size was uint8_t. So when I try this code:
Code:
char msg[20] = "Link quality: ";
char value[6];
(void)sprintf(value,"%c",linkQuality);
(void)strcat(msg,value);
(void)Uart2_Transmit(msg,70,NULL);

 The msg string contains "Link quality: F" instead of "Link quality: 240". I tried also with %d in sprintf, with no success. Maybe do I wrong sprintf call?

 

 

sshot_11.jpg
Message Edited by t.dowe on 2009-09-04 11:04 AM
0 Kudos

2,461 Views
Sten
Contributor IV
What result did you get when you tried the %d instead of %c? The %d should be the right choice if you want the number as decimal, if you want it in hexadecimal, use %x instead:
 
sprintf(msg, "Link quality: %x\n", linkQuality)
 
Sten
 
 
 
0 Kudos

2,461 Views
CecchiSandrone
Contributor II
I tried also with sprintf(msg, "Link quality: %d\n", linkQuality) but I see incorrect values. :smileymad:
0 Kudos

2,461 Views
CompilerGuru
NXP Employee
NXP Employee
sprintf is an advanced function as it provides no type checking, and for a small chip like a HC08 it is also
rather large resource wise.
The usual suspects if it does not work:
- missing include, no #include <stdio.h>
- not enough stack space.
- type of format string does not match with type actually passed.

I would suggest to use a an explicit int -> string conversion function as the Technoman64 provided, it
fixes most of the issues of sprintf.

Daniel
0 Kudos

2,461 Views
CecchiSandrone
Contributor II
Yes with Technoman64's function my code works. Thanks
0 Kudos

2,461 Views
CecchiSandrone
Contributor II
Which type of data is represented by uint8_t? Isn't it a char?
0 Kudos

2,461 Views
Sten
Contributor IV
You can't convert a number to text like that in C; the (char *) cast only claims to the compiler that the following identifier actually contains a pointer to a character (string). You must use an explicite function like sprintf or itoa (don't know if itoa exists in CW) to convert the number to a string.
Sten
 
0 Kudos