Advertisement
R1bell

Untitled

Oct 5th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14.  
  15. namespace WpfApplication1
  16. {
  17.     /// <summary>
  18.     /// Interaction logic for MainWindow.xaml
  19.     /// </summary>
  20.     public partial class MainWindow : Window
  21.     {
  22.         public MainWindow()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void convertButton_Click(object sender, RoutedEventArgs e)
  28.         {
  29.             // Get the integer entered by the user
  30.             int i;
  31.             if (!int.TryParse(inputTextBox.Text, out i))
  32.             {
  33.                 MessageBox.Show("TextBox does not contain an integer");
  34.                 return;
  35.             }
  36.  
  37.             // Check that the user has not entered a negative number
  38.             if (i < 0)
  39.             {
  40.                 MessageBox.Show("Please enter a positive number or zero");
  41.                 return;
  42.             }
  43.  
  44.             // Remainder will hold the remainder after dividing i by 2
  45.             // after each iteration of the algorithm
  46.             int remainder = 0;
  47.  
  48.             // Binary will be used to construct the string of bits
  49.             // that represent i as a binary value
  50.             StringBuilder binary = new StringBuilder();
  51.  
  52.             // Generate the binary representation of i
  53.             do
  54.             {
  55.                 remainder = i % 2;
  56.                 i = i / 2;
  57.                 binary.Insert(0, remainder);
  58.             }
  59.             while (i > 0);
  60.  
  61.             // Display the result
  62.             binaryLabel.Content = binary.ToString();
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement