user481610 user481610 - 1 month ago 8
Swift Question

How to print the content of a variable type Data using swift?

All I am looking to is take a string and get its hex value. I've been following this post, here is the code I have in the playground:

let str = "Say Hello to My Little Friend"
let data = str.data(using:String.Encoding.utf16)
print("\(data!)")


However, I just printed out this:


"60 bytes\n"


. How do I get the hex value of:


5361792048656c6c6f20746f204d79204c6974746c6520467269656e64


Answer

Just

print("\(data! as NSData)")

PS: Your expected hex is .utf8

Comments