ocaml convert string UTF8 to CP1251 - encoding

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

Related

What is the relevance of this String format specifier?

I'm trying to get an understanding of some code I came across recently.
In an answer to a question here https://stackoverflow.com/a/51173170/1162328, the author made use of a String with a format specifier when looping over files in the documentDirectory. Can anyone shed some light on what %#/%# is actually doing?
for fileName in fileNames {
let tempPath = String(format: "%#/%#", path, fileName)
// Check for specific file which you don't want to delete. For me .sqlite files
if !tempPath.contains(".sql") {
try fileManager.removeItem(atPath: tempPath)
}
}
Reading the Apple documentation archive for Formatting Basics I came across this:
In format strings, a ‘%’ character announces a placeholder for a value, with the characters that follow determining the kind of value expected and how to format it. For example, a format string of "%d houses" expects an integer value to be substituted for the format expression '%d'. NSString supports the format characters defined for the ANSI C functionprintf(), plus ‘#’ for any object.
What exactly then, is %#/%# doing?
Each format specifier is replaced by one of the following arguments (usually in the same order, although that can be controlled with positional arguments). So in your case, the first %# is replaced by path and the second %# is replaced by fileName. Example:
let path = "/path/to/dir"
let fileName = "foo.txt"
let tempPath = String(format: "%#/%#", path, fileName)
print(tempPath) // /path/to/dir/foo.txt
The preferred way to build file names and paths is to use the corresponding URL methods instead of string manipulation. Example:
let pathURL = URL(fileURLWithPath: path)
let tempURL = pathURL.appendingPathComponent(fileName)
if tempURL.pathExtension != "sql" {
try FileManager.default.removeItem(at: tempURL)
}
%# is something similar to %d or anything like that. This is the way of string interpolation in Swift.
To be exact %# is placeholder for object - used in Objective-C A LOT. Since NSString * was object (now it is only String), it was used to insert NSString * into another NSString *.
Also given code is just rewritten objective-c code which was something like
NSString *tempPath = [NSString stringWithFormat:#"%#/%#", path, filename];
which can be rewritten in swift:
let tempPath = path + "/" + fileName
Also, given path = "Test" and fileName = "great" will give output Test/great.
One more note: %# is as good as dangerous. You can put UITableView as well as String in it. It will use description property for inserting into string.

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

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 ??

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?