c 轉bcd碼 BCD碼 十六進製制與十進位制互轉

2021-10-17 06:11:01 字數 3362 閱讀 4670

在做嵌入式軟體的設計中,經常會遇到十六進製制、bcd碼與十進位制之間的轉換,最近做m1卡的應用中,涉及了大量的十六進製制、bcd碼與十進位制之間的轉換。筆者通過對bcd碼、十六進製制 權的理解,輕鬆的實現了他們之間的互換。

#include

#include

//功能:二進位製取反

//輸入:const unsigned char *src  二進位制資料

//      int length                待轉換的二進位制資料長度

//輸出:unsigned char *dst        取反後的二進位制資料

//返回:0    success

int convert(unsigned char *dst, const unsigned char *src, int length)

int i;

for(i=0; i

dst[i] = src[i]^0xff;

return 0;

//功能:十六進製制轉為十進位制

//輸入:const unsigned char *hex         待轉換的十六進製制資料

//      int length                       十六進製制資料長度

//輸出:

//返回:int  rslt                        轉換後的十進位制資料

//思路:十六進製制每個字元位所表示的十進位制數的範圍是0 ~255,進製為256

//      左移8位(<<8)等價乘以256

unsigned long hextodec(const unsigned char *hex, int length)

int i;

unsigned long rslt = 0;

for(i=0; i

rslt += (unsigned long)(hex[i])<

return rslt;

//功能:十進位制轉十六進製制

//輸入:int dec                     待轉換的十進位制資料

//      int length                  轉換後的十六進製制資料長度

//輸出:unsigned char *hex          轉換後的十六進製制資料

//返回:0    success

//思路:原理同十六進製制轉十進位制

int dectohex(int dec, unsigned char *hex, int length)

int i;

for(i=length-1; i>=0; i--)

hex[i] = (dec%256)&0xff;

dec /= 256;

return 0;

//功能:求權

//輸入:int base                    進製基數

//      int times                   權級數

//輸出:

//返回:unsigned long               當前資料位的權

unsigned long power(int base, int times)

int i;

unsigned long rslt = 1;

for(i=0; i

rslt *= base;

return rslt;

//功能:bcd轉10進製

//輸入:const unsigned char *bcd     待轉換的bcd碼

//      int length                   bcd碼資料長度

//輸出:

//返回:unsigned long               當前資料位的權

//思路:壓縮bcd碼乙個字元所表示的十進位制資料範圍為0 ~ 99,進製為100

//      先求每個字元所表示的十進位制值,然後乘以權

unsigned long  bcdtodec(const unsigned char *bcd, int length)

int i, tmp;

unsigned long dec = 0;

for(i=0; i

tmp = ((bcd[i]>>4)&0x0f)*10 + (bcd[i]&0x0f);

dec += tmp * power(100, length-1-i);

return dec;

//功能:十進位制轉bcd碼

//輸入:int dec                      待轉換的十進位制資料

//      int length                   bcd碼資料長度

//輸出:unsigned char *bcd           轉換後的bcd碼

//返回:0  success

//思路:原理同bcd碼轉十進位制

int dectobcd(int dec, unsigned char *bcd, int length)

int i;

int temp;

for(i=length-1; i>=0; i--)

temp = dec%100;

bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0f);

dec /= 100;

return 0;

int main()

register int i;

unsigned char tmp_bff[12] = "";

//十六進製制轉十進位制

unsigned char hex[4] = ;

unsigned long dec_hex = 0;

dec_hex = hextodec(hex, 4);

printf("dec_hex = %d\n", dec_hex);

//十進位制轉十六進製制

dectohex(dec_hex, tmp_bff, 4);

for(i=0; i<5; i++)

printf("tmp_bff[%d] = 0x%02x\n",i, tmp_bff[i]);

//bcd碼轉十進位制

unsigned long dec_bcd = 0;

unsigned char bcd[4] = ;

dec_bcd = bcdtodec(bcd, 4);

printf("dec_bcd = %d\n", dec_bcd);

//十進位制轉bcd碼

dectobcd(dec_bcd, tmp_bff, 4);

for(i=0; i<5; i++)

printf("tmp_bff[%d] = 0x%02x\n", i, tmp_bff[i]);

getchar();

BCD碼與十六進製制值轉換

bcd碼中最常用的就是8421型bcd碼數值,儀器為例使用以下方法進行轉換 bcd 碼 轉 十進位制 define bcd to decimal x x 0xf0 4 10 x 0x0f 十進位制 轉 bcd 碼 define decimal to bcd x x 10 4 x 10 brief 十...

BCD碼 十六進製制 十進位制 互轉

include include 功能 二進位製取反 輸入 const unsigned char src 二進位制資料 int length 待轉換的二進位制資料長度 輸出 unsigned char dst 取反後的二進位制資料 返回 0 success int convert unsigned ...

BCD碼 十六進製制與十進位制互轉

在做嵌入式軟體的設計中,經常會遇到十六進製制 bcd碼與十進位制之間的轉換,最近做m1卡的應用中,涉及了大量的十六進製制 bcd碼與十進位制之間的轉換。筆者通過對bcd碼 十六進製制 權的理解,輕鬆的實現了他們之間的互換。include include 功能 二進位製取反 輸入 const unsi...