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
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts