THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial explains about following things in Swift
Sometimes, We need to convert Data to/from Hexa string in Swift.
Data contains a string representation of bytes Hexa is a string that contains 0 to 9 and a to g.
In this example, Convert data to a Hexa decimal string
Here is an example
import Foundation
let str = "onetwothree"
let data = Data(str.utf8)
print(data) // 11 bytes
let hexaValue = data.map{ String(format:"%02x", $0) }.joined()
print(hexaValue) // 6f6e6574776f7468726565
Output:
11 bytes
6f6e6574776f7468726565
In this example, Convert the Hexa decimal string to a Data object using the Data constructor with the hexString attribute
Here is a code example
let dataObject = Data(hexString: "abc11d")
print(dataObject?.hexString) // Optional("abc11d")
It is an easy and quick way to convert Hexa to data
Learned how to convert data to Hexa and Hexa decimal to Data in swift with code examples
🧮 Tags
Recent posts
Nodejs package.json resolutions How to find Operating System username in NodeJS? How to convert Double to Integer or Integer to double in Dart| Flutter By Example Ways to skip test case execution in Gradle project build Learn Gradle | tutorials, and examplesRelated posts