C/C++-將一個十六進制的字符串型的數字轉換成整型數字.

int str2Hex( char *pstr)
{
    int ans = 0;
    char *pt;
    pt = pstr;
	if( !pstr )
	{
		return 0;
	}
    while( *pt )
    {  
	ans = ans<<4;
        if( ( *pt >=  'A' && *pt <=  'F' ) || ( *pt >=  'a' && *pt <=  'f' ) )
               ans  = ans | ((*pt & 0x5f) -0x37);
        else
               ans  = ans | (*pt) -0x30;
        pt++;
    }
    return ans;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章