⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 numconv.c

📁 MSP430电能测量程序,用的是电力线载波通讯.即PLC
💻 C
字号:
int Divide_ul_By10(unsigned long* n)
  {
  unsigned long t;
  unsigned long mod = *n;               // copy dividend into modulo resultant

  *n = 0;                               // clear the quotient result
  
  for(t = 0xA0000000ul; t > 5; t >>= 1) // loop divisor through all columns
    {
    *n <<= 1;                           // shift up the quotient
    if(mod >= t)                        // if we can subtract out a 10 from this column
      {
      *n |= 1;                          // indicate so in the quotient
      mod -= t;                         // subtract out this column
      }
    }
  
  return (int)mod;                      // return the modulo value, result is returned in pointer passed
  }


int ul_to_a(unsigned long n, char* buff)
  {
  char* p = buff;
  char  t;
  int   cnt = 0;
  
  if( n == 0 )                          // if this number is zero
    {
    *(buff++) = '0';                    // assign a zero to the buffer
    *buff     = 0;                      // assign the null terminator
    return 1;                           // exit the routine
    }

  while( n > 0 )                        // while there are digits to determine
    {
    *(buff++) = '0' + Divide_ul_By10(&n); // get the next digit
    cnt++;                              // bump the character counter
    }
  
  *(buff--) = 0;                        // assign the null terminator, adjust buffer pointer to last character
    
  // at this point, the number is there but represented in reverse form so swap the characters
  while( p < buff )                     // while there are characters to swap
    {
    t = *p;                             // copy the lead character
    *(p++) = *buff;                     // move the trailing character to the lead character
    *(buff--) = t;                      // copy the old lead character to the trailing charactor
    }

  return cnt;                           // return the number of characters converted
  }


int l_to_a(long n, char* buff)
  {
  int cnt = 0;

  if( n < 0 )                           // if a negative number passed
    {
    *(buff++) = '-';                    // assign the negative symbol
    n = -n;                             // make the number positive
    cnt = 1;                            // count the negative sign
    }

  cnt += ul_to_a((unsigned long)n, buff); // get the digits of the number and string length

  return cnt;                           // return the number of characters converted
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -