
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
Swift Program to convert Hexadecimal to Decimal
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.
- Related Articles
- Swift Program to convert Decimal to Hexadecimal
- Haskell Program to convert Decimal to Hexadecimal
- Golang Program to convert Decimal to Hexadecimal
- Golang Program to convert Hexadecimal to Decimal
- Haskell Program to convert Hexadecimal to Decimal
- Java Program to convert from decimal to hexadecimal
- Java program to convert decimal number to hexadecimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert decimal integer to hexadecimal number
- How to Convert Decimal to Hexadecimal?
- How to Convert Hexadecimal to Decimal?
- Swift Program to convert Decimal to Octal
- Swift Program to convert Decimal to Binary
- Swift Program to convert Binary to Decimal
- How to convert Decimal to Hexadecimal in Java
