关闭

16进制码转换ASCII码

754人阅读 评论(0) 收藏 举报

//----------------------------------------------------------------------
//16进制码转换ASCII码
//----------------------------------------------------------------------
int MN2_KNL_HexToAscii( P_U8 pAscTxt, P_U8 pHexTxt, int iHexTxtLen )
{
    register P_U8   pAscStr;
    register P_U8   pHexCur;
    P_U8            pHexEnd;


    SCI_ASSERT( pAscTxt );
    SCI_ASSERT( pHexTxt );
    SCI_ASSERT( !(iHexTxtLen & 1) );
    pAscStr = pAscTxt;
    pHexCur = pHexTxt;
    pHexEnd = pHexCur + iHexTxtLen;
    while(pHexCur < pHexEnd)
    {
        //填写ASCII码的高4位
        if( ('0' <= *pHexCur) && ('9' >= *pHexCur) )
        {
            *pAscStr = (*pHexCur & 0x0F) << 4;
        }
        else if( ('A' <= *pHexCur) && ('F' >= *pHexCur) )
        {
            *pAscStr = (*pHexCur - 'A' + 10) << 4;
        }
        else if( ('a' <= *pHexCur) && ('f' >= *pHexCur) )
        {
            *pAscStr = (*pHexCur - 'a' + 10) << 4;
        }
        pHexCur++;

        //填写ASCII码的低4位
        if( ('0' <= *pHexCur) && ('9' >= *pHexCur) )
        {
            *pAscStr += (*pHexCur & 0x0F);
        }
        else if( ('A' <= *pHexCur) && ('F' >= *pHexCur) )
        {
            *pAscStr += (*pHexCur - 'A' + 10);
        }
        else if( ('a' <= *pHexCur) && ('f' >= *pHexCur) )
        {
            *pAscStr += (*pHexCur - 'a' + 10);
        }
        pHexCur++;

        pAscStr++;
    }

    return (int)(iHexTxtLen / 2);
}
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14955次
    • 积分:305
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类