zoukankan      html  css  js  c++  java
  • 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"); }
  • 相关阅读:
    每日编程-20170322
    每日编程-20170321
    C++primer拾遗(第七章:类)
    每日编程-20170320
    uniApp之 顶部选项卡
    vue 服务端渲染 vs 预渲染(1)
    uni-app学习笔记
    如何解决vue跨域的问题
    简单整理数组的用法
    vue-cli
  • 原文地址:https://www.cnblogs.com/ytjjyy/p/2525966.html
Copyright © 2011-2022 走看看