Hex Conversion

      My first blog, I had the idea of ​​blogging a long time ago, but I only implemented it today. The procrastination is late and I have to change it! !

Let’s talk about the topic first. I just saw this topic and I was very confused. I didn’t understand English well and couldn’t understand it (ashamed). Alright, let's get to the point. I won't explain too much about the base conversion, just go to the code

 
 
    public string HexConversion(int n, int k)
    {
        if (n == 0) return "0"; //special case 0
        string res = null;
        int remain = 0;//Remainder
        while (n > 0)
        {
            char c;
            remain = n % k;
            if (remain <= 9)
            {
                c = (char)('0' + remain);//The ASC code value is obtained by adding characters and integers (basically know, often review)   
            }
            else
            {
                c = (char)('A' + (remain - 10));
            }
            res = c + res;
            n /= k;
        }
        return res;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982895&siteId=291194637