Cannot encode string to utf8 and utf8 to base64 in swift - swift

I'm looking for a way to encode a string to UTF8 and then to base 64 in Swift 3.x
In swift 2.x I was using this way :
let pass: NSString = "test"
let variable: NSString = (pass.dataUsingEncoding(NSUTF8StringEncoding)?.base64EncodedStringWithOptions([]))!
XCode force me to put : NSUTF8StringEncoding.rawValue and then the result is not correct.
If someone has a solution :)
Thank

In Swift 3, it's neater now:
let pass = "test"
let variable = pass.data(using: .utf8)!.base64EncodedString()
And you might want to use better named variables than variable.
If you need to deal with NSString:
let pass: NSString = "test"
let variable = (pass as String).data(using: .utf8)!.base64EncodedString()

Related

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"

Swift, stringWithFormat, %s gives strange results

I searched for an answer the entire day but nothing really came close to answering my issue. I am trying to use stringWithFormat in Swift but while using printf format strings. The actual issue I have is with the %s. I can't seem to get to the original string no matter how I try this.
Any help would be much much appreciated (or workarounds).
Things I already did: tried all available encodings for the cString, tried creating an ObjC function to use for this, but when I passed the arguments from Swift I ran into the same strange issue with the %s, even if when hardcoded in the ObjC function body it appears to print the actual correct String.
Please find bellow the sample code.
Many thanks!
var str = "Age %2$i, Name: %1$s"
let name = "Michael".cString(using: .utf8)!
let a = String.init(format: str, name, 1234)
Expected result is quite clear I presume, however I get something like this instead of the correct name:
"Age 1234, Name: ÿQ5"
Use withCString() to invoke a function with the C string
representation of a Swift string. Also note that %ld is the correct
format for a Swift Int (which can be a 32-bit or 64-bit integer).
let str = "Age %2$ld, Name: %1$s"
let name = "Michael"
let a = name.withCString { String(format: str, $0, 1234) }
print(a) // Age 1234, Name: Michael
Another possible option would be to create a (temporary) copy
of the C string representation
(using the fact a Swift string is automatically converted to a C string when passed to a C function taking a const char * parameter,
as explained in String value to UnsafePointer<UInt8> function parameter behavior):
let str = "Age %2$ld, Name: %1$s"
let name = "Michael"
let nameCString = strdup(name)!
let a = String(format: str, nameCString, 1234)
print(a)
free(nameCString)
I assume that your code does not work as expected because name
(which has type [CChar] in your code) is bridged to an NSArray,
and then the address of that array is passed to the string
formatting method.
Use "%1$#" instead of "%1$s", and don't use the cString call.
This works for me:
var str = "Age %2$i, Name: %1$#"
let name = "Michael"
let a = String.init(format: str, name, 1234)

Swift: How to convert a String to UInt8 array?

How do you convert a String to UInt8 array?
var str = "test"
var ar : [UInt8]
ar = str
Lots of different ways, depending on how you want to handle non-ASCII characters.
But the simplest code would be to use the utf8 view:
let string = "hello"
let array: [UInt8] = Array(string.utf8)
Note, this will result in multi-byte characters being represented as multiple entries in the array, i.e.:
let string = "é"
print(Array(string.utf8))
prints out [195, 169]
There’s also .nulTerminatedUTF8, which does the same thing, but then adds a nul-character to the end if your plan is to pass this somewhere as a C string (though if you’re doing that, you can probably also use .withCString or just use the implicit conversion for bridged C functions.
let str = "test"
let byteArray = [UInt8](str.utf8)
swift 4
func stringToUInt8Array(){
let str:String = "Swift 4"
let strToUInt8:[UInt8] = [UInt8](str.utf8)
print(strToUInt8)
}
I came to this question looking for how to convert to a Int8 array. This is how I'm doing it, but surely there's a less loopy way:
Method on an Extension for String
public func int8Array() -> [Int8] {
var retVal : [Int8] = []
for thing in self.utf16 {
retVal.append(Int8(thing))
}
return retVal
}
Note: storing a UTF-16 encoded character (2 bytes) in an Int8 (1 byte) will lead to information loss.

ocaml convert string UTF8 to CP1251

Help me write correct code, I need convert string from utf8 to cp1251. Using the library Uutf.
my code is not work
let str = "русский текст" in
let decode = Uutf.encoding_of_string str in
Uutf.encoding_to_string decode;;
I found another solution. Convert string via library Tk.
open Tk;;
let top = openTk ();;
let str = "abracadabra" in
let x = Encoding.convertfrom ~encoding:"utf-8" abracadabra in
print_endline(x);;
Neither Batteries Included nor Uutf handles the CP1251 encoding, as far as I can tell. You might look at Camomile.
(It's interesting to ask yourself what encoding is being used in the quoted text of your source code.)

Replace all Special Characters in String with valid URL characters

I cannot figure out how to replace all special characters in a string and convert it to a string I can use in a URL.
What I am using it for:
I am uploading an image, converting it to base64, and then passing it to the Laravel framework, however the base64 string can contain +, /, \, etc. which changes the meaning of the URL.
I can replace the + sign with the following code:
let withoutPlus = image.stringByReplacingCharactersInRange("+", withString: "%2B")
however then I cannot use that as a NSString to try and change the other characters.
Surely there is a way to just target every single special character and convert it something usable in a URL?
You can use stringByAddingPercentEncodingWithAllowedCharacters to escape characters as needed. You pass it an NSCharacterSet containing the characters that are valid for that string (i.e. the ones you don't want replaced). There's a built-in NSCharacterSet for characters allowed in URL query strings that will get you most of the way there, but it includes + and / so you'll need to remove those from the set. You can do that by making a mutable copy of the set and then calling removeCharactersInString:
let allowedCharacters = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet
allowedCharacters.removeCharactersInString("+/=")
Then you can call stringByAddingPercentEncodingWithAllowedCharacters on your string, passing in allowedCharacters:
let encodedImage = image.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacters)
Note that it will return an optional String (String?) so you'll probably want to use optional binding:
if let encodedImage = image.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacters) {
/* use encodedImage here */
} else {
/* stringByAddingPercentEncodingWithAllowedCharacters failed for some reason */
}
Example:
let unencodedString = "abcdef/+\\/ghi"
let allowedCharacters = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as NSMutableCharacterSet
allowedCharacters.removeCharactersInString("+/=")
if let encodedString = unencodedString.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacters) {
println(encodedString)
}
Prints:
abcdef%2F%2B%5C%2Fghi
Use
let withoutPlus = image.stringByReplacingOccurrencesOfString("+", withString: "%2B")
rather than image.stringByReplacingCharactersInRange. Note that your call as posted doesn't work, as that method is declared as
func stringByReplacingCharactersInRange(range: Range<String.Index>, withString replacement: String) -> String
and you are not supplying the correct parameters.
You might do better to use POST to send a file, rather than encode it into your URL