一个文本转2进制的函数
发布时间:2006-10-14 3:03:09   收集提供:gaoqian

看到前面有人要,我在这里贴一个函数。
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 ;
}

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50