【C语言】常见数据格式转换处理的代码实现

版权声明:本文为博主【北纬22.5度的攻城狮】原创文章,转载请务必注明出处! https://blog.csdn.net/szullc/article/details/85009998

笔者在日常工作中开展项目开发,经常遇到要将数据格式做转换处理,比如一段字符串转换为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;
    }
}

大家如对实现代码有疑问,欢迎在评论席发言。 @_@ ...

猜你喜欢

转载自blog.csdn.net/szullc/article/details/85009998
今日推荐