首页 > Linux操作系统 > Linux操作系统 > 一个文本转2进制的函数(转)
看到前面有人要,我在这里贴一个函数。
int hextobin(char *shex ,char *pbuf ,int ilen)
{
unsigned char shexstr[3] ;
int ierr=0 ;
memset(shexstr,0x0,3) ;
for( int i=0;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 ;
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-125452/ ,如需转载,请注明出处,否则将追究法律责任。