一个文本转2进制的函数

发表于:2007-06-23来源:作者:点击数: 标签:
看到前面有人要,我在这里贴一个函数。 int HexToBin(char *sHex ,char *pBuf ,int ilen) { unsigned char sHexStr[3] ; int iErr=0 ; memset(sHexStr,0x0,3) ; for( int i=0;iilen;i++) { memcpy(sHexStr,sHex[i*2],2) ; if( sHexStr[0]='0' sHexStr[0]='9'

   

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

原文转自:http://www.ltesting.net