Convert Binary Number to Decimal Number in Windows Store App

Introduction

My previous article was Convert Decimal to Binary Number and in this article I explain how to convert a number expressed as a binary number to a number expressed as a decimal number. So let us first of all understand the conversion.

Binary Number: A number expressed using the base 2 number with digits 0 and 1.

Decimal Number: A number expressed using the 10 number with digits 0 to 9.

Conversion Example: Suppose we want to convert binary number 1101 to decimal. The followiing is the conversion process:

Step 1: 1* 1=1
Step 2: 0* 2=0
Step 3: 1*4=4
Step 4: 1*8=8

Its decimal value = 1 + 0 + 4 + 8 = 13.

So (1101)2  =  (13)10

Use the following procedure to create it.

Step 1

First of all you must create a new Windows Store Application.

Open Visual Studio 2012
"File" -> "New" -> "Project..."
Choose "Template" -> "Visual C#" -> "Window Store app"
Choose "Blank App (XAML)" then rename the application

new-windows-store-app.jpg

Step 2

Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="binary_to_decimal_convertor_app.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:binary_to_decimal_convertor_app"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid Background="red">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="313*"/>

            <ColumnDefinition Width="254*"/>

            <ColumnDefinition Width="788*"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="78*"/>

            <RowDefinition Height="49*"/>

            <RowDefinition Height="37*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="39*"/>

            <RowDefinition Height="525*"/>

        </Grid.RowDefinitions>

        <TextBlock Text="Binary to Decimal Converter app" Grid.ColumnSpan="3" Grid.Row="1" Foreground="White" FontSize="20" FontFamily="Arial" TextAlignment="Center"></TextBlock>

        <TextBlock Text="Enter Binary Number :" FontFamily="Arial" FontSize="15" Foreground="White" Grid.Column="1" Grid.Row="2" Grid.RowSpan="4"></TextBlock>

        <TextBox x:Name="textbox1" Grid.Column="2" Grid.Row="2" Width="200" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" />

        <TextBlock Text="Eqivalent Decimal Number " FontSize="15" Foreground="White" Grid.Column="1" Grid.Row="3"></TextBlock>

        <TextBlock x:Name="text1" FontSize="15" Foreground="White" Grid.Column="2" Grid.Row="3" Width="200" HorizontalAlignment="Left" ></TextBlock>

        <Button x:Name="button1" Grid.Column="2" Grid.Row="4" Background="Yellow" Foreground="Black" Width="150" Content="Click" Click="button1_Click"></Button>

 

    </Grid>

</Page>

Step 3

Now write the following C# code for the button within "Mainpage.Xaml.cs".

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace binary_to_decimal_convertor_app

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            long binum=0;

            int r=0, j=1;

            int decnum=0;

            binum =long.Parse(textbox1 .Text );

            while (binum !=0)

            {

                r= (int) binum % 10;

                decnum =decnum +r*j;

                j=j*2;

                binum =binum /10;

            }

            text1 .Text =decnum .ToString ();

        }

    }

}


Step 4

Now Run your app.

output-of-decimal-app.jpg


Similar Articles