Advertisement
vlad0

Numeral 3

Jan 15th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03.DecimalToHex
  7. {
  8.     class Program
  9.     {
  10.         static char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  11.         static void Main(string[] args)
  12.         {
  13.             int decimalNumber=-12345678;
  14.             //Console.WriteLine("Enter decimal number: ");
  15.             //decimalNumber = int.Parse(Console.ReadLine());
  16.             bool isNegative = decimalNumber < 0 ? true : false;
  17.             string hexNumber = ToHex(decimalNumber, isNegative);
  18.             Console.WriteLine(hexNumber);
  19.  
  20.         }
  21.  
  22.         private static string ToHex(int decimalNumber, bool isNegative)
  23.         {
  24.             StringBuilder result = new StringBuilder();
  25.  
  26.             if (isNegative)
  27.             {
  28.                 decimalNumber *= -1; //get the positive value
  29.                
  30.                 //get the length of the bits in decimalNumber without left zeroes
  31.                 int length = Convert.ToString(decimalNumber,2).Length;
  32.                 //we have to split the binary code by 4 bits
  33.                 int addition=0;
  34.                 if (length%4 !=0)
  35.                 {
  36.                     //get the difference between length and 4 if it doesn't devide exactly
  37.                     addition = 4 - length % 4;
  38.                 }
  39.                 int lengthBits = length + addition;
  40.                 //if the bits aren't exactly lengthBits we fill with zeroes from the left
  41.                 string bits = Convert.ToString(decimalNumber,2);
  42.                 //we get the new decimalNumber
  43.                 decimalNumber = Convert.ToInt32(bits, 2);
  44.                
  45.                 //invert the bits till lengthbits with the XOR operator
  46.                 int mask = 1;
  47.                 for (int i = 0; i < lengthBits; i++)
  48.                 {
  49.                     mask = 1<<i;
  50.                     decimalNumber = decimalNumber ^ mask;
  51.                 }
  52.                 //we add + 1
  53.                 decimalNumber += 1;
  54.             }
  55.  
  56.             while (decimalNumber != 0)
  57.             {
  58.                 //match the remaining with the coresponding element in the hex array
  59.                 result.Append(hexArray[decimalNumber % 16]);
  60.                 decimalNumber /= 16;
  61.                
  62.             }
  63.  
  64.             string hex = result.ToString();
  65.             int lengthHex = hex.Length;
  66.             result.Clear();
  67.             //reverse the array in the right way
  68.             for (int i = lengthHex-1; i >= 0; i--)
  69.             {
  70.                 result.Append(hex[i]);
  71.             }
  72.  
  73.             if (isNegative)
  74.             {
  75.                 hex = result.ToString();
  76.                 result.Clear();
  77.                 //fill the left spaces with F
  78.                 result.Append(hex.PadLeft(8,'F'));    
  79.             }
  80.  
  81.             return result.ToString();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement