Convert integer to hexadecimal and back again

前端 未结 10 2172
轮回少年
轮回少年 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:33

    NET FRAMEWORK

    Very well explained and few programming lines GOOD JOB

    // Store integer 182
    int intValue = 182;
    // Convert integer 182 as a hex in a string variable
    string hexValue = intValue.ToString("X");
    // Convert the hex string back to the number
    int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
    

    PASCAL >> C#

    http://files.hddguru.com/download/Software/Seagate/St_mem.pas

    Something from the old school very old procedure of pascal converted to C #

        /// 
        /// Conver number from Decadic to Hexadecimal
        /// 
        /// 
        /// 
        public string MakeHex(int w)
        {
            try
            {
               char[] b =  {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
               char[] S = new char[7];
    
                  S[0] = b[(w >> 24) & 15];
                  S[1] = b[(w >> 20) & 15];
                  S[2] = b[(w >> 16) & 15];
                  S[3] = b[(w >> 12) & 15];
                  S[4] = b[(w >> 8) & 15];
                  S[5] = b[(w >> 4) & 15];
                  S[6] = b[w & 15];
    
                  string _MakeHex = new string(S, 0, S.Count());
    
                  return _MakeHex;
            }
            catch (Exception ex)
            {
    
                throw;
            }
        }
    

提交回复
热议问题