How can I print the content of a variable of type Data using Swift? [duplicate] - ios

This question already has answers here:
How to convert Data to hex string in swift
(8 answers)
Closed 6 years ago.
All I am looking to do is take a string and get its hex value. I've been following this post. Here is the code I have in my playground:
let str = "Say Hello to My Little Friend"
let data = str.data(using: String.Encoding.utf16)
print("\(data!)")
However, my code just prints:
"60 bytes\n"
How can I print the hex value? For reference, it should be:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64

Since Data is a Sequence of UInt8, you could map each byte to a string and then join them:
data.map { String(format: "%02x", $0) }.joined()

Just
print(data! as NSData)
PS: Your expected hex is .utf8

Related

Percent encoding output incorrect in Swift 4 for '(apostrophe) [duplicate]

This question already has answers here:
Swift - encode URL
(19 answers)
Closed 3 years ago.
I am encoding my string to hit a web service. I am using addingPercentEncoding(withAllowedCharacters: CharacterSet) to encode my String.
Everything works fine except the '(apostrophe) character gets encoded to %E2%80%99 instead of %27.
if let _keyword = keyword?
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
parameters?["keyword"] = _keyword
}
For Example:
When keyword is Maggie's, output = "Maggie%E2%80%99s" instead of "Maggie%27s".
Output is fine for others, when keyword is Jelly Extracts output is "Jelly%20Extracts".
So, how do I encode '(apostrophe) properly to %27
Edit: When I pass static text, like "Maggie's.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)" output is correct but when I wrap it in a variable output comes incorrect.
Instead of urlQueryAllowed use alphanumerics.
let originalString = "Maggie's"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
print(escapedString) // Optional("Maggie%27s")

how can I convert String to Data [duplicate]

This question already has answers here:
Creating NSData from NSString in Swift
(9 answers)
Closed 4 years ago.
I have next:
deviceName = String.init(bytes: temp.prefix(upTo: index), encoding: .windowsCP1251)
where temp - [UInt8]. My question is: how can I convert this string back to Data?
I'm trying to convert like this:
newDataName = Data(newName.windowsCP1251)
But result is:
Value of type String has no member windowsCP1251
It is works with converting using utf8, but it shows russian characters incorrect. I need to use windowsCP1251 only:
newDataName = Data(newName.utf8)
Maybe this is what you looking for:
newDataName = newName.data(using: .windowsCP1251)

C# - How to decode UTF-8 String to Normal Strings or Emoji [duplicate]

This question already has answers here:
How to decode UTF8 bytes?
(2 answers)
Closed 5 years ago.
I storing my messages as utf-8 to preserve emoji
but how do I decode it again and show its original form
This is the Sample Message - %e2%9c%8c
String result = java.net.URLDecoder.decode(contacts.getYourmessage(), "UTF-8");
This is the Sample Message - %e2%9c%8c
It is url-encoded string. So you can
var str = WebUtility.UrlDecode("%e2%9c%8c");
which returns ✌
This is for swift:
var str = "%e2%9c%8c"
print(str.removingPercentEncoding)
Output:
Optional("✌")

How to generate random code mixed of numbers and letters - Swift [duplicate]

This question already has answers here:
Generate random alphanumeric string in Swift
(24 answers)
Closed 6 years ago.
I'm trying to generate random code mixed of numbers (0-9) and letters(A-z) so I can send it to user email to use it when login .
Is there a library or any way to do this ?
This is give random value...
func returnUUIDWithTimeStamp() -> String {
return UUID().uuidString + NSString(format: "%f", Date().timeIntervalSince1970 * 1000).substring(with: NSRange(location: 8, length: 5))
}
Result will contain '-'
C95B920A-3866-4A50-A783-D719FB0B607512830

Swift : How to put string in \( )? [duplicate]

This question already has answers here:
include dictionary or array value in swift string with backslash notation [duplicate]
(2 answers)
Closed 8 years ago.
I create a dictionary, and I want to use println() to print dictionary content,
but it doesn't work, and I don't know how to solve it..
Please help... Thanks
var dictionary = ["name":"herny","age":20]
println("name = \(dictionary["name"])"); <--- this doesn't work, compile error because dictionary["name"] in \()
You can't put things with quotes in a \( ), you'll need to assign it to a variable:
var dictionary = ["name": "henry", "age": "20"];
let name = dictionary["name"];
println("name = \(name)");

Resources