How to convert numbers between hexadecimal and decimal

前端 未结 17 1078
情歌与酒
情歌与酒 2020-11-22 05:30

How do you convert between hexadecimal numbers and decimal numbers in C#?

相关标签:
17条回答
  • 2020-11-22 06:13

    Convert binary to Hex

    Convert.ToString(Convert.ToUInt32(binary1, 2), 16).ToUpper()
    
    0 讨论(0)
  • 2020-11-22 06:15
    String stringrep = myintvar.ToString("X");
    
    int num = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);
    
    0 讨论(0)
  • 2020-11-22 06:17

    Hex -> decimal:

    Convert.ToInt64(hexValue, 16);
    

    Decimal -> Hex

    string.format("{0:x}", decValue);
    
    0 讨论(0)
  • 2020-11-22 06:17

    An extension method for converting a byte array into a hex representation. This pads each byte with leading zeros.

        /// <summary>
        /// Turns the byte array into its Hex representation.
        /// </summary>
        public static string ToHex(this byte[] y)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in y)
            {
                sb.Append(b.ToString("X").PadLeft(2, "0"[0]));
            }
            return sb.ToString();
        }
    
    0 讨论(0)
  • 2020-11-22 06:18

    This is not really easiest way but this source code enable you to right any types of octal number i.e 23.214, 23 and 0.512 and so on. Hope this will help you..

        public string octal_to_decimal(string m_value)
        {
            double i, j, x = 0;
            Int64 main_value;
            int k = 0;
            bool pw = true, ch;
            int position_pt = m_value.IndexOf(".");
            if (position_pt == -1)
            {
                main_value = Convert.ToInt64(m_value);
                ch = false;
            }
            else
            {
                main_value = Convert.ToInt64(m_value.Remove(position_pt, m_value.Length - position_pt));
                ch = true;
            }
    
            while (k <= 1)
            {
                do
                {
                    i = main_value % 10;                                        // Return Remainder
                    i = i * Convert.ToDouble(Math.Pow(8, x));                   // calculate power
                    if (pw)
                        x++;
                    else
                        x--;
                    o_to_d = o_to_d + i;                                        // Saving Required calculated value in main variable
                    main_value = main_value / 10;                               // Dividing the main value 
                }
                while (main_value >= 1);
                if (ch)
                {
                    k++;
                    main_value = Convert.ToInt64(Reversestring(m_value.Remove(0, position_pt + 1)));
                }
                else
                    k = 2;
                pw = false;
                x = -1;
            }
            return (Convert.ToString(o_to_d));
        }    
    
    0 讨论(0)
提交回复
热议问题