Swift Program to convert Binary to Decimal


This tutorial will discuss how to write Swift program to convert Binary to Decimal number.

Decimal numbers are those numbers whose base value is 10. Decimal numbers are also known as base-10 number system which contain 10 numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Here, the position of every digit in the decimal number has weight is a power of 10. For example, (89)10 = 8 x 101 + 9 x 100.

Binary numbers are those numbers whose base value is 2. Binary numbers are also known as base-16 number system which contain only two number 1 and 0. Here, the position of every digit in the binary number has weight is a power of 2.

For example, (1011)2 is a binary number. Binary numbers are the most commonly used number system in computer devices where every digit is represented by a bit.

Now we convert the binary(base-2) into decimal(base-10)number using any of the following methods.

Below is a demonstration of the same −

Input

Suppose our given input is −

Binary number = 11011

Output

The desired output would be −

Decimal number = 27

Method1: CONVERT DECIMAL TO BINARY NUMBER BY POWER OF 2

Example 

We can also convert decimal to binary number by extracting each digit from the number and then multiply the extracted digit by base(power of 2) and add to the decimal variable. At the end of the program you will get all the decimal digits in the decimal variable.

For Example −

Binary number = 101 
Decimal Number = 1 * 22 + 0 * 21 + 1 * 20 = 4 + 0 + 1 = 5

Example

The following program shows how to convert binary to decimal number.

import Foundation 
import Glibc

// Binary number 
var number = 101101 
var decimalNum = 0 
var baseVal = 1

print("Binary Number: ", number)

// Converting binary into decimal 
while(number > 0){
   // Extraction rightmost with the help 
   // of remainder 
   let endVal = number % 10 
   number /= 10

   // Multiple the extracted digit with the base(power of 2) 
   // and then add it to the decimalNum 
   decimalNum += endVal * baseVal
   baseVal *= 2 
} 
print("Decimal Number: ", decimalNum)

Output

Binary Number: 101101 
Decimal Number: 45

Method 2 - CONVERT BINARY TO DECIMAL NUMBER SYSTEM USING INT(_:RADIX:).

We can also convert binary to decimal number system using Int(_:radix:). This method creates a new value from the given string/number and radix.

Syntax

Following is the syntax −

Int(value, radix: base)

Here, value is the ASCII representation of a number. Whereas radix is used to convert text into integer value. The default value of radix is 10 and it can be in the range from 2…36.

Example

The following program shows how to convert binary to decimal number.

import Foundation 
import Glibc

// Decimal number 
var number = "11110"

print("Binary Number:", number)

// Converting binary to decimal number 
let DecimalNumber = Int(number, radix: 2)!

print("Decimal Number:", DecimalNumber)

Output

Binary Number: 11110 Decimal Number: 30

Here, we convert the binary number 11110 into decimal number using the following code −

let DecimalNumber = Int(number, radix: 2)!

Where Int(number, radix: 2)! convert the given binary number into decimal number. Hence the resultant decimal number is 30.

Updated on: 30-Nov-2022

772 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements