Base64Encoding result string with "/" in Swift - swift

I want to convert a UIImage to a string representation. I am using the following code:
let imageData = UIImagePNGRepresentation(resizedImage)
if let imageBase64 = imageData?.base64EncodedDataWithOptions(NSDataBase64EncodingOptions (rawValue: 0)) {
let strBase64:String = imageBase64.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
}
The resulting string looks something like:
aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQWZRQUFBRk5DQUlBQUFCNWNRcGdBQUFBQVhOU1IwSUFyczRjNlFBQUFCeHBSRTlVQUFBQUFnQUFBQUFBQUFDbkFBQUFLQUFBQUtjQUFBQ21BQUg4T0dIYnkwY0FBRUFBU1VSQlZIZ0J0TDMxbHh4SHR1L3JQK25kYzk5ZDU3NHpaK0I0NXN5Y0FkUFlIbnRNR3RzeWcyekpZdWdXWTNjTEd0UnFadTVxWm1abVptWUd0Vmp2RzdFemQwVmxabFZYeXpOZTN4VnJaMlJXU2ZybFUxOS9ZMGZrUzdQTEQrZFdIczZ2UGxwWWZieTQ5bVJwL2NueStxT1ZqY2ZRNnVZVGFHM3JxZFRqOWUwbkxyUzI5ZGdnRncrdmJ6LzdtZHE0LzN4bDQxRjljOXZBK0p5enI5TC81dlQzZHpyU3Y1SCtzU3Y0OTI0OFc5dDRqa0xxMmNxbVhjc2J6d3hhV245cTBOemEwL24xWnhnTm1sMTk0a3d6cTQ5VlRhODhZVTB0UDJ
But the format I am looking for should contain "/", like the following (a random image I found):
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUSEhMWFRUWGBcZGRgXFxcVFRgYFxUWFhcVFxgYHSggGBolGxcXITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGhAQGisdHx0tLS0tKy0tLS0tLSstLS0tLS0tLS0tKy0tLS0tLSstLS0tLSs3Ky0tKysrLSstKysrK//AABEIAMwAzAMBIgACEQEDEQH/xAAcAAABBAMBAAAAAAAAAAAAAAAGAgMEBQABBwj/xABCEAABAwIDBQUGAwYFBAMBAAABAgMRAAQSITEFBkFRYRMicYGRBzJCobHRFCPBUmKCk+HwM0NTctIVkqLxRGOyJP/
I don't know how to get the second format (containing the "/").

You're encoding it twice. Just do
let strBase64 = imageData?.base64EncodedStringWithOptions([])

Related

Using Swift .split to format an array

I am reading in a text file of translation pairs of this format:
boy:garçon
garçon:boy
Into an array using the following code:
var vocab:[String:String] = [:]
let path = Bundle.main.path(forResource: "words_alpha", ofType: "txt")!
let text = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
let vocab = text.components(separatedBy: CharacterSet.newlines)
The imported array looks like this:
["boy:garçon", "garçon:boy"]
Whereas I would like the array to be formatted like this:
["boy":"garçon", "garçon":"boy"]
What is the best way to achieve the desired array format shown above using a Swift string transformation?
Have been trying to use .split, but with not much success.
Let's be clear:
["boy":"garçon", "garçon":"boy"]
That's a Dictionary, not an Array.
There a multiples ways to do that, here's two possible codes:
var manual: [String: String] = [:]
array.forEach { aString in
let components = aString.components(separatedBy: ":")
guard components.count == 2 else { return }
manual[components[0]] = components[1]
}
print(manual)
or
let reduced = array.reduce(into: [String: String]()) { result, current in
let components = current.components(separatedBy: ":")
guard components.count == 2 else { return }
result[components[0]] = components[1]
}
print(reduced)
Output (for both):
$> ["garçon": "boy", "boy": "garçon"]
As said, it's a Dictionary, so there is no guarantee that the print be:
["garçon": "boy", "boy": "garçon"] or ["boy":"garçon", "garçon":"boy"], it's key-value access, not index-value access.

Trying to concatenate a string with what I think is an unicode suffix

I have this app of mine that reads datamatrix barcodes from drugs using the camera.
When it does for a particular drug, I receive this string from the detector, as seen on Xcode console:
0100000000D272671721123110700XXXX\U0000001d91D1
my problem is that \U0000001d91D1 part.
This code can be decomposed on the following:
01 00000000D27267 17 211231 10 700XXXX \U0000001d 91D1"
01 = drug code
17 = expiring date DMY
10 = batch number
The last part is the dosage rate
Now on another part of the application I am on the simulator, with no camera, so I need to pass this string to the module that decomposes the code.
I have tried to store the code as a string using
let code = "0100000000D272671721123110700XXXX\U0000001d91D1"
it complains about the inverted bar, so I change it to double bar
let code = "0100000000D272671721123110700XXXX\\U0000001d91D1"
the detector analyzes this string and concludes that the batch number is 700XXXX\U0000001d91D1, instead of just 700XXXX, so the information contained from the \ forward is lost.
I think this is unicode or something.
How do I create this string correctly.
You can use string transform to decode your hex unicode characters:
let str1 = #"0100000000D272671721123110700XXXX\U00000DF491D1"#
let str2 = #"0100000000D272671721123110700XXXX\U0000001d91D1"#
let decoded1 = str1.applyingTransform(.init("Hex-Any"), reverse: false)! // "0100000000D272671721123110700XXXX෴91D1"
let decoded2 = str2.applyingTransform(.init("Hex-Any"), reverse: false)! // "0100000000D272671721123110700XXXX91D1"
You can also get rid of the verbosity extending StringTransform and StringProtocol:
extension StringTransform {
static let hexToAny: Self = .init("Hex-Any")
static let anyToHex: Self = .init("Any-Hex")
}
extension StringProtocol {
var decodingHex: String {
applyingTransform(.hexToAny, reverse: false)!
}
var encodingHex: String {
applyingTransform(.anyToHex, reverse: false)!
}
}
Usage:
let str1 = #"0100000000D272671721123110700XXXX\U00000DF491D1"#
let str2 = #"0100000000D272671721123110700XXXX\U0000001d91D1"#
let decoded1 = str1.decodingHex // "0100000000D272671721123110700XXXX෴91D1"
let decoded2 = str2.decodingHex // "0100000000D272671721123110700XXXX91D1"
The \U0000001d substring probably represents code point U+001D INFORMATION SEPARATOR THREE, which is also the ASCII code point GS (group separator).
In a Swift string literal, we can write that code point using a Unicode escape sequence: \u{1d}. Try writing your string literal like this:
let code = "0100000000D272671721123110700XXXX\u{1d}91D1"

Using String addingPercentEncoding to encode a string as query param

I have an input string "+20" and I am trying to pass that as query parameter in url.
So I am trying to encode the myInputString by doing
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
But in the debugger, the string s1 still shows as '+20' instead of '%2B20'
Is there something I did wrong?
As already mentioned by matt + is a legal URL character. If you really need to encode it you would need to create your own custom urlQueryAllowed and subtract the plus sign from it:
extension CharacterSet {
static let allowedCharacters = urlQueryAllowed.subtracting(.init(charactersIn: "+"))
}
let myInputString = "+20"
let s1 = myInputString.addingPercentEncoding(withAllowedCharacters: .allowedCharacters) // "%2B20"

character extract from string in swift

I have a task to do something with a chess board. The input gives us a starting position of some chess figure. For example "b4" or "a6" or something like that. How can i decompose the input and make from it two integer numbers, like in C++:
string input;
cin>>input
int coord_x = input[0] - 'a';
int coord_y = input[1]
I cannot manage to do that in swift. I do something like:
let input : String=readLine()!
let characters = Array(input)
and then try to take the int but it doesnt work, no matter what i try...
and what type is the content of the Array in swift?
You can retrieve the c string representation like this:
let string = "a5"
let scalars = string.lowercased().cString(using: .ascii)!
let first = scalars[0]
let second = scalars[1]
It could be safer to retrieve the unicodeScalar characters instead:
let string = "a5".lowercased()
let characters = Array(string.unicodeScalars)
let first = characters[0].value - UnicodeScalar(unicodeScalarLiteral: "a").value

Formatting an Array

This formats my emoji perfectly:
lblEmoji!.text = String(format: "%C", 0xe04f)
However, as soon as I change the string to my son array it does not format correctly as an emoji
lblEmoji!.text = questionArray.objectAtIndex(currQuestionCount-1).valueForKey("quesimage") as? String;(format: "%C", 0xe04f)
Where is it I am going wrong?
String;(format: "%C", 0xe04f) is wrong. I don't even know what you're trying to do here.
If the objects in questionArray have a String variable quesimage (which is your emoji value), why not just take that directly without initializing a new String?
if let str = questionArray.objectAtIndex(currQuestionCount-1).valueForKey("quesimage") as? String {
lblEmoji!.text = str
}
or if you need to use a format string:
lblEmoji!.text = String(format: "%C", str)
If quesimage is a regular String representation of a UTF8 string, use a format string like so:
let utf8str = "0xe04f" // your value coming from quesimage
let emojiStr = String(format: "%C", arguments: [utf8str])
print(emojiStr) // ⑰
lblEmoji!.text = emojiStr