• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            唐吉訶德

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              5 Posts :: 75 Stories :: 3 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(2)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            做嵌入式軟件的設(shè)計(jì)中,經(jīng)常會(huì)遇到十六進(jìn)制、BCD碼與十進(jìn)制之間的轉(zhuǎn)換,最近做M1卡的應(yīng)用中,涉及了大量的十六進(jìn)制、BCD碼與十進(jìn)制之間的轉(zhuǎn)換。筆者通過對BCD碼、十六進(jìn)制 權(quán)的理解,輕松的實(shí)現(xiàn)了他們之間的互換。
            #include <stdio.h>
            #include <string.h
            >
            /////////////////////////////////////////////////////
            //
            //功能:二進(jìn)制取反
            //
            //輸入:const unsigned char *src  二進(jìn)制數(shù)據(jù)
            //      int length                待轉(zhuǎn)換的二進(jìn)制數(shù)據(jù)長度
            //
            //輸出:unsigned char *dst        取反后的二進(jìn)制數(shù)據(jù)
            //
            //返回:0    success
            //
            //////////////////////////////////////////////////////
            int convert(unsigned char *dst, const unsigned char *src, int length)
            {
                    int i;
                    for(i=0; i<length; i++)
                    {
                            dst[i] = src[i]^0xFF;
                    }
                    return 0;
            }
            //////////////////////////////////////////////////////////
            //
            //功能:十六進(jìn)制轉(zhuǎn)為十進(jìn)制
            //
            //輸入:const unsigned char *hex         待轉(zhuǎn)換的十六進(jìn)制數(shù)據(jù)
            //      int length                       十六進(jìn)制數(shù)據(jù)長度
            //
            //輸出:
            //
            //返回:int  rslt                        轉(zhuǎn)換后的十進(jìn)制數(shù)據(jù)
            //
            //思路:十六進(jìn)制每個(gè)字符位所表示的十進(jìn)制數(shù)的范圍是0 ~255,進(jìn)制為256
            //      左移8位(<<8)等價(jià)乘以256
            //
            /////////////////////////////////////////////////////////
            unsigned long HextoDec(const unsigned char *hex, int length)
            {
                int i;
                unsigned long rslt = 0;
                for(i=0; i<length; i++)
                {
                    rslt += (unsigned long)(hex[i])<<(8*(length-1-i));
                                                                    
                }
                return rslt;
            }

            /////////////////////////////////////////////////////////
            //
            //功能:十進(jìn)制轉(zhuǎn)十六進(jìn)制
            //
            //輸入:int dec                     待轉(zhuǎn)換的十進(jìn)制數(shù)據(jù)
            //      int length                  轉(zhuǎn)換后的十六進(jìn)制數(shù)據(jù)長度
            //
            //輸出:unsigned char *hex          轉(zhuǎn)換后的十六進(jìn)制數(shù)據(jù)
            //
            //返回:0    success
            //
            //思路:原理同十六進(jìn)制轉(zhuǎn)十進(jìn)制
            //////////////////////////////////////////////////////////
            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;
            }
            /////////////////////////////////////////////////////////
            //
            //功能:求權(quán)
            //
            //輸入:int base                    進(jìn)制基數(shù)
            //      int times                   權(quán)級數(shù)
            //
            //輸出:
            //
            //返回:unsigned long               當(dāng)前數(shù)據(jù)位的權(quán)
            //
            //////////////////////////////////////////////////////////
            unsigned long power(int base, int times)
            {
                int i;
                unsigned long rslt = 1;
                for(i=0; i<times; i++)
                    rslt *= base;
                return rslt;
            }
            /////////////////////////////////////////////////////////
            //
            //功能:BCD轉(zhuǎn)10進(jìn)制
            //
            //輸入:const unsigned char *bcd     待轉(zhuǎn)換的BCD碼
            //      int length                   BCD碼數(shù)據(jù)長度
            //
            //輸出:
            //
            //返回:unsigned long               當(dāng)前數(shù)據(jù)位的權(quán)
            //
            //思路:壓縮BCD碼一個(gè)字符所表示的十進(jìn)制數(shù)據(jù)范圍為0 ~ 99,進(jìn)制為100
            //      先求每個(gè)字符所表示的十進(jìn)制值,然后乘以權(quán)
            //////////////////////////////////////////////////////////
            unsigned long  BCDtoDec(const unsigned char *bcd, int length)
            {
                 int i, tmp;
                 unsigned long dec = 0;
                 for(i=0; i<length; i++)
                 {
                    tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);   
                    dec += tmp * power(100, length-1-i);          
                 }
                 return dec;
            }
            /////////////////////////////////////////////////////////
            //
            //功能:十進(jìn)制轉(zhuǎn)BCD碼
            //
            //輸入:int Dec                      待轉(zhuǎn)換的十進(jìn)制數(shù)據(jù)
            //      int length                   BCD碼數(shù)據(jù)長度
            //
            //輸出:unsigned char *Bcd           轉(zhuǎn)換后的BCD碼
            //
            //返回:0  success
            //
            //思路:原理同BCD碼轉(zhuǎn)十進(jìn)制
            //
            //////////////////////////////////////////////////////////
            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] = "";
                //十六進(jìn)制轉(zhuǎn)十進(jìn)制
                unsigned char HEX[4] = {0x34, 0xFE, 0x3E, 0xFF};
                unsigned long dec_hex = 0;
                dec_hex = HextoDec(HEX, 4);
                
                printf("dec_hex = %d\n", dec_hex);
                //十進(jìn)制轉(zhuǎn)十六進(jìn)制
                DectoHex(dec_hex, tmp_bff, 4);
                for(i=0; i<5; i++)
                {
                    printf("tmp_bff[%d] = 0x%02X\n",i, tmp_bff[i]);
                }
                //BCD碼轉(zhuǎn)十進(jìn)制
                unsigned long dec_bcd = 0;
                unsigned char BCD[4] = {0x98, 0x23, 0x45, 0x78};
                dec_bcd = BCDtoDec(BCD, 4);
                printf("dec_bcd = %d\n", dec_bcd);
                //十進(jìn)制轉(zhuǎn)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

             

             

            void BCDToChar(const unsigned char *src,unsigned int srcLen, unsigned char *dest)
            {
                unsigned 
            char temp[2= {0};
                unsigned 
            char temp2[1= {0};
                
            for(int i=0; i<srcLen; i++)
                {
                   temp2[
            0= src[i];

                   temp[
            0= src[i]>>4;
                   temp[
            0= temp[0]&0x0F;
                   temp[
            0= temp[0]+0x30;

                   temp[
            1= temp2[0]&0x0F;
                   temp[
            1= temp[1]+0x30;

                   dest[i
            *2= temp[0];
                   dest[i
            *2+1= temp[1];
                }
            }
            posted on 2011-01-21 14:04 心羽 閱讀(10030) 評論(1)  編輯 收藏 引用 所屬分類: C/C++

            Feedback

            # re: BCD碼、十六進(jìn)制與十進(jìn)制互轉(zhuǎn) 2013-11-12 16:40 hustcalm
            讀了文章有一定的啟發(fā)。
            不過與其轉(zhuǎn)成hex string做轉(zhuǎn)換,更加優(yōu)雅且有效的方式應(yīng)該是利用類型轉(zhuǎn)換,
            拿unsigned char HEX[4] = {0x34, 0xFE, 0x3E, 0xFF}為例:
            signed long sLong = 0x34 << 24 | 0xFE << 16 | 0x3E << 8 | 0xFF

            如果想得到unsigned long,只需要改變數(shù)據(jù)類型即可:
            unsigned long sLong = 0x34 << 24 | 0xFE << 16 | 0x3E << 8 | 0xFF

            如果是雙字節(jié)hex,則需要通過signed short和unsigned short進(jìn)行轉(zhuǎn)換。
              回復(fù)  更多評論
              

            久久精品国产2020| 婷婷久久综合九色综合绿巨人 | 久久精品无码一区二区无码| 无码久久精品国产亚洲Av影片| 久久99热只有频精品8| 青青草国产成人久久91网| 色老头网站久久网| 欧美粉嫩小泬久久久久久久 | 93精91精品国产综合久久香蕉| 久久久亚洲精品蜜桃臀| 人妻精品久久久久中文字幕69| 久久久91精品国产一区二区三区| 一本久久a久久精品综合香蕉| 久久w5ww成w人免费| 性做久久久久久久久老女人| 亚洲AV日韩精品久久久久久| 日韩亚洲国产综合久久久| 99久久久国产精品免费无卡顿| 久久久无码精品亚洲日韩软件| 国产精品视频久久| AV无码久久久久不卡蜜桃| 精品乱码久久久久久夜夜嗨| 激情伊人五月天久久综合| 天堂久久天堂AV色综合| 奇米影视7777久久精品人人爽| 97久久精品人人做人人爽| 欧美精品一本久久男人的天堂| 人妻无码αv中文字幕久久| 99精品国产免费久久久久久下载 | 国产精品99久久久久久董美香| 亚洲AV日韩精品久久久久久| 久久精品国产精品亚洲精品| 亚洲国产精品成人久久蜜臀| 久久久久亚洲AV无码专区网站 | 久久精品九九亚洲精品天堂| 久久人人爽人人爽人人AV东京热 | 99久久精品国产综合一区| 99久久精品国产麻豆| 欧美综合天天夜夜久久| 国产亚洲精午夜久久久久久| 国产综合成人久久大片91|