ekostadinov

Convert decimal to 256base

Feb 22nd, 2013
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1.                                              
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Numerics;
  8. using System.IO;
  9.  
  10. namespace ProvadiaNumbers
  11. {
  12.     class NumsToProvadia
  13.     {
  14.         static string[] table = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  15.         "aA", "aB", "aC", "aD", "aE", "aF", "aG", "aH", "aI", "aJ", "aK", "aL", "aM", "aN", "aO", "aP", "aQ", "aR", "aS", "aT", "aU", "aV", "aW", "aX", "aY", "aZ",
  16.         "bA", "bB", "bC", "bD", "bE", "bF", "bG", "bH", "bI", "bJ", "bK", "bL", "bM", "bN", "bO", "bP", "bQ", "bR", "bS", "bT", "bU", "bV", "bW", "bX", "bY", "bZ",
  17.         "cA", "cB", "cC", "cD", "cE", "cF", "cG", "cH", "cI", "cJ", "cK", "cL", "cM", "cN", "cO", "cP", "cQ", "cR", "cS", "cT", "cU", "cV", "cW", "cX", "cY", "cZ",
  18.         "dA", "dB", "dC", "dD", "dE", "dF", "dG", "dH", "dI", "dJ", "dK", "dL", "dM", "dN", "dO", "dP", "dQ", "dR", "dS", "dT", "dU", "dV", "dW", "dX", "dY", "dZ",
  19.         "eA", "eB", "eC", "eD", "eE", "eF", "eG", "eH", "eI", "eJ", "eK", "eL", "eM", "eN", "eO", "eP", "eQ", "eR", "eS", "eT", "eU", "eV", "eW", "eX", "eY", "eZ",
  20.         "fA", "fB", "fC", "fD", "fE", "fF", "fG", "fH", "fI", "fJ", "fK", "fL", "fM", "fN", "fO", "fP", "fQ", "fR", "fS", "fT", "fU", "fV", "fW", "fX", "fY", "fZ",
  21.         "gA", "gB", "gC", "gD", "gE", "gF", "gG", "gH", "gI", "gJ", "gK", "gL", "gM", "gN", "gO", "gP", "gQ", "gR", "gS", "gT", "gU", "gV", "gW", "gX", "gY", "gZ",
  22.         "hA", "hB", "hC", "hD", "hE", "hF", "hG", "hH", "hI", "hJ", "hK", "hL", "hM", "hN", "hO", "hP", "hQ", "hR", "hS", "hT", "hU", "hV", "hW", "hX", "hY", "hZ",
  23.         "iA", "iB", "iC", "iD", "iE", "iF", "iG", "iH", "iI", "iJ", "iK", "iL", "iM", "iN", "iO", "iP", "iQ", "iR", "iS", "iT", "iU", "iV"
  24.                                 };                                                                                        
  25.  
  26.         static void Main()
  27.         {
  28.             BigInteger decNumber = BigInteger.Parse(Console.ReadLine());
  29.                        
  30.             if (decNumber <= 255)
  31.             {                
  32.                 Console.WriteLine(table[(int)decNumber]);                            
  33.             }
  34.             else //numbers bigger than table
  35.            {
  36.                //int numLen = finalNum.Length - 1;  //needed to find final number pow                    
  37.  
  38.                BigInteger current = 0;
  39.                BigInteger reminder = decNumber % 256;
  40.                StringBuilder finalNum = new StringBuilder();
  41.  
  42.                while (decNumber != 0)
  43.                {
  44.                    current = decNumber / 256;
  45.                    if (current <= 255 && current > 0)
  46.                    {
  47.                        finalNum.Append(table[(int)current]);
  48.  
  49.                        if (reminder <= 255)
  50.                        {
  51.                            finalNum.Append(table[(int)reminder]);
  52.                        }                  
  53.                    }                                      
  54.                    
  55.                    decNumber = current;
  56.                }
  57.  
  58.                Console.WriteLine(finalNum);
  59.          
  60.             }                      
  61.  
  62.            
  63.              
  64.         }
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment