首页 > 学院 > 网络通信 > 正文

一个文本转2进制的函数

2019-11-04 22:07:04
字体:
来源:转载
供稿:网友

看到前面有人要,我在这里贴一个函数。
int HexToBin(char *sHex ,char *pBuf ,int ilen)
{
    unsigned char sHexStr[3] ;
    int iErr=0 ;
    memset(sHexStr,0x0,3) ;
    for( int i=0;i<ilen;i++)
    {
        memcpy(sHexStr,&sHex[i*2],2) ;
        if( sHexStr[0]>='0' && sHexStr[0]<='9' )
            sHexStr[0]=sHexStr[0]-'0' ;        
        else
            if( sHexStr[0]>='A' && sHexStr[0]<='F' )
                sHexStr[0]=sHexStr[0]-'A'+10 ;        
            else
            {
                sHexStr[0] = 0 ;
                iErr++ ;
            }

        if( sHexStr[1]>='0' && sHexStr[1]<='9' )
            sHexStr[1]=sHexStr[1]-'0' ;        
        else
            if( sHexStr[1]>='A' && sHexStr[1]<='F' )
                sHexStr[1]=sHexStr[1]-'A'+10 ;        
            else
            {
                sHexStr[1] = 0 ;
                iErr++ ;
            }    
        pBuf[i] = sHexStr[0]*16 + sHexStr[1] ;
    }
    return iErr ;
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表