how to convert fileurl(swift) to encoded utf8 hex? - swift

i just want to convert fileURLWithPath to utf8 hex format as like bellow swift code .
import Cocoa
// this swift file name is aaa.swift
let arguments = CommandLine.arguments
let str = "\(arguments)"
let url = URL(fileURLWithPath: str)
print(url)
and i send bash path variable to aaa.swift file . like below
path="/Volumes/aaa/ccc.mov"
swift aaa.swift $path
then print out on terminal like below
%5B%22./aaa.swift%22,%20%22/Volumes/aaa/ccc.mov%22%5D -- file:///Volumes/aaa/
i want to get "/Volumes/aaa/ccc.mov" this part ..
how to do this ??

Related

How to append a string to a URL in Swift?

Given a name of a file like this:
let fileName = "example.jpg"
I am grabbing the URL of this directory using a helper function, which returns the URL to me:
let imagesURL = getURL()
How can I append the string fileName to the imagesURL such that it returns something like this:
/path/to/images/dir/example.jpg
How do I get it in both String and URL format?
This gives you a new URL with the fileName appended:
let appended = imagesURL.appendingPathComponent(fileName)
And this converts it back to a string:
let strVersion = appended.absoluteString //full URL
let strVersion2 = appended.path //path only
url.appendingPathComponent(fileName)
has been deprecated, use
url.appending(path: filename) instead.

Urlencode cyrillic characters in Swift

I need to convert a cyrillic string to its urlencoded version using Windows-1251 encoding. For the following example string:
Моцарт
The correct result should be:
%CC%EE%F6%E0%F0%F2
I tried addingPercentEncoding(withAllowedCharacters:) but it doesn't work.
How to achieve the desired result in Swift?
NSString has a addingPercentEscapes(using:) method which allows to specify an arbitrary
encoding:
let text = "Моцарт"
if let encoded = (text as NSString).addingPercentEscapes(using: String.Encoding.windowsCP1251.rawValue) {
print(encoded)
// %CC%EE%F6%E0%F0%F2
}
However, this is deprecated as of iOS 9/macOS 10.11. It causes compiler warnings and may not be available in newer OS versions.
What you can do instead is to convert the string do Data with
the desired encoding,
and then convert each byte to the corresponding %NN sequence (using the approach from
How to convert Data to hex string in swift):
let text = "Моцарт"
if let data = text.data(using: .windowsCP1251) {
let encoded = data.map { String(format: "%%%02hhX", $0) }.joined()
print(encoded)
// %CC%EE%F6%E0%F0%F2
}

Cannot encode string to utf8 and utf8 to base64 in 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()

Swift Base64 Format

On this website, for example when you drop in an image, it is turned into a proper base64 format: http://base64image.org/
In my Swift app, here is what I have to generate a base64 string from an image:
let image_Data = UIImagePNGRepresentation(default_image)
let base64String = image_Data!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
This gives me something similar, but it has spaces and line breaks. How do I get an output just like the output from the website above?
let image_Data = UIImagePNGRepresentation(default_image)
let base64String = image_Data!.base64EncodedStringWithOptions([]) // Don't ask for line breaks
If you remove the request for line breaks, does it match what you're expecting?

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.)