Advertisement
MichalProkop

Integer conversion

May 6th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 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 ConConversion
  8. {
  9.     class Program
  10.     {
  11.         //Again I needed to change this method to static method, so we can get input paramether from Main method
  12.         //I didnt use Convert or Pharse, just simply use ASCII enconding
  13.         //By ASCII enconding we are avoiding the undesirable characteristics
  14.         //All will be changed to number even numbers
  15.         //I am sure, that I could use here Try and Cath, and make exception (outOfRange) for undesirable characteristics
  16.         //But still I think this is better way
  17.         static public int ConvertToInt(string value)
  18.         {
  19.             int sum = 0;
  20.             byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
  21.             foreach (byte b in asciiBytes)
  22.             {
  23.                 //Writing each number for every characteristic
  24.                 Console.Write(b + ", ");
  25.                 //In the end I sum all numbers to one
  26.                 sum += b;
  27.             }
  28.             Console.WriteLine();
  29.             Console.WriteLine("And the finally number is:" + sum);
  30.  
  31.             return sum;
  32.         }
  33.  
  34.         static void Main(string[] args)
  35.         {
  36.             string value = "";
  37.             Console.WriteLine("Write your numbers/characters.");
  38.             value = Convert.ToString(Console.ReadLine());
  39.             ConvertToInt(value);
  40.            
  41.  
  42.             //I am sure, that I could by this check if are there any letter, if result==true, I shold close application.
  43.             /*bool result = !value.Any(x => char.IsLetter(x));
  44.             if (result == true)
  45.             Console.WriteLine("True");
  46.             else
  47.             Console.WriteLine("False"); */
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement