C++ char转十六进制


int hexCharToInt(char c)  
{   
        if (c >= '0' && c <= '9') return (c - '0');  
        if (c >= 'A' && c <= 'F') return (c - 'A' + 10);  
        if (c >= 'a' && c <= 'f') return (c - 'a' + 10);  
        return 0;  
}  
  
char* hexstringToBytes(string s)  
{           
        int sz = s.length();  
        char *ret = new char[sz/2];  
        for (int i=0 ; i <sz ; i+=2) {  
            ret[i/2] = (char) ((hexCharToInt(s.at(i)) << 4)  
                                | hexCharToInt(s.at(i+1)));  
        }  
        return ret;  
}  
  
string bytestohexstring(char* bytes,int bytelength)  
{  
  string str("");  
  string str2("0123456789abcdef");   
   for (int i=0;i<bytelength;i++) {  
     int b;  
     b = 0x0f&(bytes[i]>>4);  
     char s1 = str2.at(b);  
     str.append(1,str2.at(b));            
     b = 0x0f & bytes[i];  
     str.append(1,str2.at(b));  
     char s2 = str2.at(b);  
   }  
   return str;  
}  
  
int main()  
{  
        char s[3] ={'a','b','c'};  

        std::string result;

        result = bytestohexstring(s,strlen(s));  

        for(int i=0;i<3;i++)
        {
            printf("%02x", s[i]&0xFF);
        }

        printf("\n%s\n",result.data());

        system("pause"); }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM