using System; using System.Text; class DecToHex { static void Main(string[] args) { long num = 100000; Console.WriteLine(DecimalToHex(num)); // 186A0 } private static string DecimalToHex(long num) { var res = new StringBuilder(); while (num > 0) { var reminder = num % 16; if (reminder > 9) { res.Insert(0, (char)(reminder + 55)); } else { res.Insert(0, reminder); } num /= 16; } return res.ToString(); } }
Tag: decimal
How to convert decimal to binary number in C# using method
using System; using System.Text; class DecToBin { static void Main(string[] args) { long num = int.MaxValue * 2L; Console.WriteLine(DecimalToBinary(num)); // 11111111111111111111111111111110 } private static string DecimalToBinary(long num) { StringBuilder res = new StringBuilder(); while (num > 0) { res.Insert(0, num % 2); num /= 2; } return res.ToString(); } }
How to convert binary number to decimal in C# using method.
using System; class BinToDec { static void Main(string[] args) { string binary = "101"; Console.WriteLine(BinaryToDecimal(binary)); // will return 5 } private static long BinaryToDecimal(string binary) { long decimalNum = 0; for (int i = binary.Length - 1, pow = 0; i >= 0; i--, pow++) { decimalNum += int.Parse(binary[i].ToString()) * (long)Math.Pow(2, pow); } return decimalNum; } }