Advertisement
stanevplamen

NumericConvertions

May 2nd, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication8
  8. {
  9.     class NumberConvertions
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hexadecimal-and-decimal-in-c
  14.             // To convert from Decimal to Hex do...
  15.             Console.WriteLine("Моля въведете число");
  16.             int decValue = Convert.ToInt32(Console.ReadLine());
  17.             string hexValue = decValue.ToString("X");
  18.             //Convert.ToInt64(hexValue, 16);
  19.             Console.WriteLine(hexValue);
  20.  
  21.  
  22.             // To convert from Hex to Decimal do either...
  23.             int decValue2 = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
  24.             Console.WriteLine(decValue2);
  25.             //string.format("{0:x}", decValue);
  26.  
  27.             // http://stackoverflow.com/questions/2954962/decimal-to-binary-conversion-in-c
  28.             // To convert from Decimal to Binary do...
  29.             string binary = Convert.ToString(decValue, 2);
  30.             Console.WriteLine(binary);
  31.  
  32.             // To convert from Binary to Decimal do either..
  33.             // http://stackoverflow.com/questions/2954962/decimal-to-binary-conversion-in-c
  34.             long l = Convert.ToInt64(binary, 2);
  35.             Console.WriteLine(l);
  36.  
  37.             // Binary to Hex
  38.             // http://stackoverflow.com/questions/5612306/converting-long-string-of-binary-to-hex-c-sharp
  39.  
  40.             // Hex to binary
  41.             string binarystring = String.Join(String.Empty, hexValue.Select(
  42.             c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')));
  43.             Console.WriteLine(binarystring);
  44.  
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement