
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Hexadecimal to Decimal in Swift
This tutorial will discuss how to write Swift program to convert Hexadecimal to Decimal.
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.
Hexadecimal numbers are those numbers whose base value is 16. Hexadecimal numbers are also known as base-16 number system which contain 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. Here, A, B, C, D, E, and F also represents decimal numbers 10, 11, 12, 13, 14, 15. Here, the position of every digit in the hexadecimal number has weight is a power of 16. For example, 2A3 is a hexadecimal number.
Hexadecimal to Decimal conversion table ?
Hexadecimal Number | Decimal Number |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Hexadecimal number = 2D
Output
The desired output would be ?
Decimal number = 45
To convert the hexadecimal(base-16) into decimal number(base-10) we use Int(_:radix:). This method create 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 Hexadecimal to decimal number.
import Foundation import Glibc // Hexadecimal number let hexaNumber = "3C5" print("Hexadecimal Number:", hexaNumber) // Converting hexadecimal number into decimal number let deciNumber = Int(hexaNumber, radix: 16)! print("Decimal Number:", deciNumber)
Output
Hexadecimal Number: 3C5 Decimal Number: 965
Here, we convert the hexadecimal number 3C5 into decimal number using the following code ?
let deciNumber = Int(hexaNumber, radix: 16)! Where Int(hexaNumber, radix: 16)! convert the given hexadecimal number into decimal number. Hence the resultant decimal number is 965.