c++ bitset


  • 0
    L
        string toHex(int num) {
            if(num==0){
                return "0";
            }
            bitset<32> bs(num);
            string s=bs.to_string();
            string res="";
            
            for(int i=0;i<32;i+=4){
                string str=s.substr(i,4);
                int a=stoi(str,nullptr,2);
                if(a<10){
                    res.push_back('0'+a);
                }else if(a>=10){
                    res.push_back('a'+a-10);
                }
            }
            return res.substr(res.find_first_not_of('0'));
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.