开发者

How to convert an int to a series of characters

I'm trying to break down an integer with C on an 8-bit microcontroller (a PIC) into its ASCII equivalent characters.

For example: convert 982 to '9','8','2'

Everything I've come u开发者_JS百科p with so far seems pretty brute force. This is the main idea of what I'm basically doing right now:

if( (10 <= n) && (n < 100) ) {
// isolate and update the first order of magnitude
digit_0 = (n % 10);
// isolate and update the second order of magnitude
switch( n - (n % 10) ) {
  case 0:
    digit_1 = 0;
    break;
  case 10:
    digit_1 = 1;
    break;

...

And then I have another function to just add 0b00110000 (48decimal) to each of my digits.

I've been having trouble finding any C function to do this for me or doing it well myself.

Thanks in advance for any help!


If sprintf isn't suitable for some reason, something simple like this would work:

char digits[MAX_DIGITS];
int count = 0;
do {
    digits[count++] = n % 10;
    n /= 10;
} while (n > 0);

There'll be count digits, with digits[0] being the least significant.


To do it yourself you need to perform the operations which are demonstrated with the sample code below:

#include <stdio.h>

int main (void)
{
  unsigned int x = 512;
  int base_val = 10, digit, i = 0, n = 0;
  char x_str[32], t;

  printf ("\nEnter an unsigned number: ");
  scanf ("%u", &x);

  printf ("\nEnter base: ");
  scanf ("%d", &base_val);

  /* Chop the digits in reverse order and store in `x_arr`
   * the interpretation of the digits are made in base value
   * denoted by `base_val`
   */

  while (x)
  {
    digit = x % base_val;
    x /= base_val;
    if (digit < 10)
      x_str[n++] = digit + '0';
    else
      x_str[n++] = digit + 'A' - 10;  /* handle base > 9 */
  }

  /* Terminate string */
  x_str[n] = '\0';

  /* Reverse string */
  for (i=0; i<n/2; i++)
  {
    t = x_str[i];
    x_str[i] = x_str[n-i-1];
    x_str[n-i-1] = t;
  }
  printf ("\n%s\n", x_str);

  return 0;
}

The while loop will chop out the digits from the integer in a given base and feed the digits in reverse order in an array. The inner if - else handles base more than 9, and places uppercase alphabets when a digit value is greater than 10. The for loop reverses the string and gets the chopped number into string in forward order.

You need to adjust the size of x_str array as per your max capability. Define a macro for it. Note the above code is only for unsigned integers. For signed integers you need to first check if it is below 0, then add a '-' sign in x_str and then print the magnitude ie. apply the above code with -x . This can also be done by checking the sign bit, by masking, but will make the process dependent on storage of integer.

The base_val is the base in which you want to interpret the numbers.


I answered a question like this a long time ago.

Implementing ftoa

I hope this helps. This applies to both integers and floating point numbers. The concept is simple.

  1. Determine number of digits (for base 10, you use log base 10)
  2. Grab the msb digit by taking the floor of (num/digit_weight)
  3. Subtract digit * weight from number and decrease weight by 1
  4. Do it again until number == 0 for integers or num > 0 + tolerance for fp numbers

Since the algorithm process a digit with every iteration, its O(logn) time so it's pretty reasonable.


If you are compatible with PIC16 RISC assembler than this is very simple, fast, short and efective. Here you have 16 bit unsigned 16 bit division by 10 rutine to see how to do it.

To get first lowest char of number in WREG put 16 bit unsigned number to Reg1 and Reg2 and call rutine. To get next char call rutine again and so on until Reg1 and Reg2 are 0.

RegAE       res 1
RegA0       res 1
RegA1       res 1
RegA2       res 1



    divR16by_c10
    ;{
    ;//Input: 16 bit unsigned number as RegA1,2 (RegA2 low byte, RegA1 High byte)
    ;//Division result: Reg1 and Reg2 and reminder as char in WREG
            clrf    RegA0
            movlw   16             ;//init loop counter
            movwf   RegAE
            lslf    RegA2, f
    divI16by_c10_        
            rlf     RegA1, f
            rlf     RegA0, f       
            movlw   10
            subwf   RegA0, f
            btfsc   Carry
            bra     divI16by_c10_OK
            addwfc  RegA0, f
            bcf     Carry
    divI16by_c10_OK        
            rlf     RegA2, f
            decfsz  RegAE, f
            bra     divI16by_c10_
    ;//result= W from 0..9
            addlw   0x30        ;//convert to char
            return 
    ;}

EDIT:

If you want to convert signed 16 bit value then check the 15th bit first, to determine sign number. If negative than write - sign and negate the number in RegA1,2. After that is the procedure the same a for positive number. To negate number you can use the following asm rutine:

             comf    RegA2, f
             comf    RegA1, f
             movlw   0
             bsf     STATUS, 0      ;//set carry flag
             addwfc  RegA2, f
             addwfc  RegA1, f 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜