【C語言】常見數據格式轉換處理的代碼實現

筆者在日常工作中開展項目開發,經常遇到要將數據格式做轉換處理,比如一段字符串轉換爲16進制的byte數組、或者一段16進制的byte數組轉換成字符串輸出等等。

現將這部分常見功能的實現分享給大家,希望對大家的學習和工作有所幫助。

//將一個雙字節(16位的數值)拆分爲一個數組按字節存儲 如 0x1234 ==> 0x12 0x34
void uint16_2_str(uint16_t Num, uint8_t *buff2)  
{
    buff2[1] = (uint8_t)Num;
    Num >>= 8;
    buff2[0] = (uint8_t)Num;
}

//將可讀的16進制串合併成其一半長度的二進制串, 如 "12AB"-->0x12AB
void asc_2_bcd(char *psIAsc, int32_t iAscLen, char *psOBcd)
{
    char   Chtmp,ChBcd;
    int32_t    iCnt;

    for(iCnt = 0; iCnt < iAscLen; iCnt += 2)
    {
        Chtmp = psIAsc[iCnt];
        if( Chtmp >= 'A' )
        {
            Chtmp = (char)toupper((int)Chtmp) - 'A' + 0x0A;
        }
        else
        {
            Chtmp &= 0x0F;
        }
        ChBcd = (Chtmp << 4); // 獲取BCD的高位

        Chtmp = psIAsc[iCnt+1];
        if( Chtmp >= 'A' )  //zyl
        {
            Chtmp = (char)toupper((int)Chtmp) - 'A' + 0x0A;
        }
        else
        {
            Chtmp &= 0x0F;
        }
        ChBcd |= Chtmp; // 獲取BCD低位

        psOBcd[iCnt/2] = ChBcd;
    }
}

//將二進制源串分解成雙倍長度可讀的16進制串, 如 0x12AB-->"12AB"
void bcd_2_asc(uint8_t *psIHex, int32_t iHexLen, char *psOAsc)
{
    static const char szMapTable[17] = {"0123456789ABCDEF"};
    int32_t   iCnt,index;
    unsigned char  ChTemp;

    for(iCnt = 0; iCnt < iHexLen; iCnt++)
    {
        ChTemp = (unsigned char)psIHex[iCnt];
        index = (ChTemp / 16) & 0x0F;
        psOAsc[2*iCnt]   = szMapTable[index];
        ChTemp = (unsigned char) psIHex[iCnt];
        index = ChTemp & 0x0F;
        psOAsc[2*iCnt + 1] = szMapTable[index];
    }
}

// 同bcd_2_asc()函數,並在目標串後添一 '\0'
void bcd_2_asc0(uint8_t *psIHex, int32_t iHexLen, char *pszOAsc)
{
    bcd_2_asc((uint8_t *)psIHex, iHexLen, pszOAsc);
    pszOAsc[2*iHexLen] = 0;
}

// 對一段字符串pszString填充前導字符ChAddChar,以便達到uiTargetLen長度
static void add_head_chars( char *pszString, int32_t iTargetLen, char ChAddChar )
{
    int32_t iLen;

    iLen = strlen((char *)pszString);
    if( iLen>=iTargetLen )
    {
        return;
    }

    memmove(pszString+iTargetLen-iLen, pszString, iLen+1);
    memset(pszString, ChAddChar, iTargetLen-iLen);
}

// 刪除一個字符串pszString中的前導字符ChRemoveChar
static void trim_head_chars(char *pszString, char ChRemoveChar)
{
    char    *p;

    if( !pszString || !*pszString )
    {
        return;
    }

    for(p=pszString; *p && *p==ChRemoveChar; p++);
    if( p!=pszString )
    {
        while( (*pszString++ = *p++) );
    }
}

//以一個字符串str,以delim爲分割符號,分割成多個字符串,返回分割後的首地址
char *my_strtok(char *src, const char *delim, char *dst)
{
    if (src && *src)
    {
        char *p = strstr(src, delim); 
        if (p)
        {
            if (dst)
            {
                memcpy(dst, src, (p-src));
            }
            return ++p;
        }
        else
        {
            if (dst)
            {
                strcpy(dst, src);
            }
            return NULL;
        }
    }
    else
    {
        return NULL;
    }
}

大家如對實現代碼有疑問,歡迎在評論席發言。 @_@ ...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章