Convert integer to hexadecimal and back again

前端 未结 10 2383
轮回少年
轮回少年 2020-11-22 02:33

How can I convert the following?

2934 (integer) to B76 (hex)

Let me explain what I am trying to do. I have User IDs in my database that are stored as inte

10条回答
  •  感动是毒
    2020-11-22 02:47

    I created my own solution for converting int to Hex string and back before I found this answer. Not surprisingly, it's considerably faster than the .net solution since there's less code overhead.

            /// 
            /// Convert an integer to a string of hexidecimal numbers.
            /// 
            /// The int to convert to Hex representation
            /// number of digits in the hex string. Pads with leading zeros.
            /// 
            private static String IntToHexString(int n, int len)
            {
                char[] ch = new char[len--];
                for (int i = len; i >= 0; i--)
                {
                    ch[len - i] = ByteToHexChar((byte)((uint)(n >> 4 * i) & 15));
                }
                return new String(ch);
            }
    
            /// 
            /// Convert a byte to a hexidecimal char
            /// 
            /// 
            /// 
            private static char ByteToHexChar(byte b)
            {
                if (b < 0 || b > 15)
                    throw new Exception("IntToHexChar: input out of range for Hex value");
                return b < 10 ? (char)(b + 48) : (char)(b + 55);
            }
    
            /// 
            /// Convert a hexidecimal string to an base 10 integer
            /// 
            /// 
            /// 
            private static int HexStringToInt(String str)
            {
                int value = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    value += HexCharToInt(str[i]) << ((str.Length - 1 - i) * 4);
                }
                return value;
            }
    
            /// 
            /// Convert a hex char to it an integer.
            /// 
            /// 
            /// 
            private static int HexCharToInt(char ch)
            {
                if (ch < 48 || (ch > 57 && ch < 65) || ch > 70)
                    throw new Exception("HexCharToInt: input out of range for Hex value");
                return (ch < 58) ? ch - 48 : ch - 55;
            }
    

    Timing code:

    static void Main(string[] args)
            {
                int num = 3500;
                long start = System.Diagnostics.Stopwatch.GetTimestamp();
                for (int i = 0; i < 2000000; i++)
                    if (num != HexStringToInt(IntToHexString(num, 3)))
                        Console.WriteLine(num + " = " + HexStringToInt(IntToHexString(num, 3)));
                long end = System.Diagnostics.Stopwatch.GetTimestamp();
                Console.WriteLine(((double)end - (double)start)/(double)System.Diagnostics.Stopwatch.Frequency);
    
                for (int i = 0; i < 2000000; i++)
                    if (num != Convert.ToInt32(num.ToString("X3"), 16))
                        Console.WriteLine(i);
                end = System.Diagnostics.Stopwatch.GetTimestamp();
                Console.WriteLine(((double)end - (double)start)/(double)System.Diagnostics.Stopwatch.Frequency);
                Console.ReadLine(); 
            }
    

    Results:

    Digits : MyCode : .Net
    1 : 0.21 : 0.45
    2 : 0.31 : 0.56
    4 : 0.51 : 0.78
    6 : 0.70 : 1.02
    8 : 0.90 : 1.25
    

提交回复
热议问题