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

📄 bcdl.c

📁 c语言库函数!里面包含了所以c语言中的系统函数的实现及其详细说明和代码!请大家及时下载!
💻 C
字号:
/* +++Date last modified: 05-Jul-1997 */

/*-----------------01-10-95 02:38pm-----------------
--  BCD conversion routines, released to the public
--  domain by Jeff Dunlop
--------------------------------------------------*/

#include "sniptype.h"         /* For True_, False_    */
#include "snipmath.h"

static int BCDLen = 8;

void SetBCDLen(int n)
{
      BCDLen = n;
}

long BCDtoLong(char *BCDNum)
{
      int i, high, low;
      Boolean_T neg;

      long total = 0;

      for (i = 0; i < BCDLen - 1; i++)
      {
            low    = 0x0f & BCDNum[i];
            high   = (0xf0 & BCDNum[i]) >> 4;
            total *= 10;
            total += high;
            total *= 10;
            total += low;
      }
      high   = (BCDNum[BCDLen - 1] & 0xf0) >> 4;
      low    = BCDNum[BCDLen - 1] & 0x0f;
      total *= 10;
      total += high;

      neg = (low == 0x0c) ? 1 : -1;

      return total * neg;
}

void LongtoBCD(long num, char BCDNum[])
{
      int i, high, low;
      Boolean_T neg = False_;

      if ( num < 0 )
      {
            neg = True_;
            num = 0 - num;
      }

      low  = neg ? 0x0d : 0x0c;
      high = (int)(num % 10);
      num /= 10;
      BCDNum[BCDLen - 1] = (char)((high << 4) | low);

      for (i = BCDLen - 2; i >= 0; i--)
      {
            low  = (int)(num % 10);
            num /= 10;
            high = (int)(num % 10);
            num /= 10;
            BCDNum[i] = (char)((high << 4) | low);
      }
}

#ifdef TEST

#include <stdio.h>

void show_BCD(char c[]);

int main(void)
{
      long a = 12345678L,
           b;
      char c[10];

      SetBCDLen(10);

      LongtoBCD(a, c);
      show_BCD(c);
      b= BCDtoLong(c);
      printf("Value is %ld\n", b);

      a = -a;
      LongtoBCD(a, c);
      show_BCD(c);
      b= BCDtoLong(c);
      printf("Value is %ld\n", b);

      return 0;
}

void show_BCD(char c[])
{
      int i;

      printf("BCD: ");
      for (i = 0; i < 10; ++i)
      {
            printf("[%d]", c[i] >> 4);
            if (9 == i)
                  printf("<sign>");
            printf("[%d]", c[i] &0xf);
      }
      puts("");
}

#endif /* TEST */

⌨️ 快捷键说明

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