Share my C solution


  • 0
    J
    char* toHex(int num) {
        unsigned int n = num;
        int size = 0;
        if (n==0) return "0";
        
        while(n>0){
            n/=16;
            size++;
        }
        n=num;
        
        char* ans = (char*) malloc(sizeof(char*)*size);
        ans[size]='\0';
        while(n>0){
            char c = n%16;
            (c<10) ? (c+='0') : (c+='a'-10);
            n=n/16;
            ans[--size] = c;
        }
        return ans;
    }
    

Log in to reply
 

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