Number to String Conversion

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

Number to String Conversion

Jump to solution
4,702 Views
Anastasios
Contributor I
Hi all,

I am using the 68HC912B32 EVB and CodeWarrior v3.1. I want to convert two numbers in one string. the first one is 1byte(unsigned char) and the other is 4(unsigned long). Is there an implementation in c I could use?

thanks for the help...
Labels (1)
Tags (1)
0 Kudos
1 Solution
927 Views
Technoman64
Contributor III

You can use sprintf function that is part of the std C lib.

Or I made the following code to build a String with a converted number added to it using the following code. The first function combines some of the standard C string functions along with the Integer to Ascii conversion. I use integers in C with assumed decimal points. This is limited as to the number of digits. The code can be modified to use long intergers if needed.

Example - I have a timer value that as an integer it is 9999. But as the user sees it after conversion it is 9.999

I define the Digits and Decimal positions

#define DELAY_TIMER_DIGITS 5

#define DELAY_TIMER_DECIMAL 1

int TimerValue = 9999;

char MyString[30];

To build a string to display to user with format of x.xxx for TimerValue

ItoaMsg(&MyString[0], "Delay Timer = ", TimerValue, DELAY_TIMER_DIGITS, DELAY_TIMER_DECIMAL);

I then print MyString on the display which would be "Delay Timer = 9.999"

void ItoaMsg(char *Buffer, char *Msg, INT16 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]);
}

/* Integer to ASCII */
void Itoa(INT16 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 */
 UINT16 uInteger;
 /* Loop index */
 INT16 Index;
 /* Number of digits converted */
 INT16 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 = ((UINT16)(-(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 = ' ';
}

View solution in original post

0 Kudos
1 Reply
928 Views
Technoman64
Contributor III

You can use sprintf function that is part of the std C lib.

Or I made the following code to build a String with a converted number added to it using the following code. The first function combines some of the standard C string functions along with the Integer to Ascii conversion. I use integers in C with assumed decimal points. This is limited as to the number of digits. The code can be modified to use long intergers if needed.

Example - I have a timer value that as an integer it is 9999. But as the user sees it after conversion it is 9.999

I define the Digits and Decimal positions

#define DELAY_TIMER_DIGITS 5

#define DELAY_TIMER_DECIMAL 1

int TimerValue = 9999;

char MyString[30];

To build a string to display to user with format of x.xxx for TimerValue

ItoaMsg(&MyString[0], "Delay Timer = ", TimerValue, DELAY_TIMER_DIGITS, DELAY_TIMER_DECIMAL);

I then print MyString on the display which would be "Delay Timer = 9.999"

void ItoaMsg(char *Buffer, char *Msg, INT16 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]);
}

/* Integer to ASCII */
void Itoa(INT16 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 */
 UINT16 uInteger;
 /* Loop index */
 INT16 Index;
 /* Number of digits converted */
 INT16 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 = ((UINT16)(-(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 = ' ';
}

0 Kudos