Java Solution


  • 0
    A

    public class Solution {

    //function to convert signed int numbers to unsigned long number
    public static long getUnsignedInt(int x) {
    return x & 0x00000000ffffffffL;
    }
    public String toHex(int num) {
    if(num == 0){
    return "0";
    }
    long rem;
    String str2="";
    char hex[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    long nums = getUnsignedInt(num);
    while(nums>0)
    {
    rem=nums%16;
    int x = (int)rem; // In array index values are int, so convert long rem to int
    str2=hex[x]+str2;
    nums=nums/16;
    }

     return str2;
    }
    

    }


Log in to reply
 

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