Converting Url to String and Back Again

Converting URL to String and back again

fileURLWithPath() is used to convert a plain file path (e.g. "/path/to/file") to an URL. Your urlString is a full URL string including the scheme, so you should use

let url = NSURL(string: urlstring)

to convert it back to NSURL. Example:

let urlstring = "file:///Users/Me/Desktop/Doc.txt"
let url = NSURL(string: urlstring)
println("the url = \(url!)")
// the url = file:///Users/Me/Desktop/Doc.txt

How to convert NSURL to String in Swift

It turns out there are properties of NSURL you can access (see Swift Reference):

var directoryURL: NSURL
var urlString: String = directoryURL.absoluteString
// OR
var urlString: String = directoryURL.relativeString
// OR
var urlString: String = directoryURL.relativePath
// OR
var urlString: String = directoryURL.path
// etc.

Converting of Uri to String

Uri to String

Uri uri;
String stringUri;
stringUri = uri.toString();

String to Uri

Uri uri;
String stringUri;
uri = Uri.parse(stringUri);

Swift How do I convert document file URLs to strings so I can display the file names in tableview

Loop through the NSURL array and do myurl.absoluteString to convert each element to a string and then append it to an array.

Problem converting link from string to urlProblem converting link from string to url

You should never force unwrap a URL created from a dynamic String which is coming from user input. You should optional bind the return value of URL(string:) and also percent encode your searchbar input to make sure the URL String is valid.

let urlString = "http://api.weatherstack.com/current?access_key=617ce097a4c8352ad4fa7b34e2570aa8&query=\(searchBar.text!)"
guard let encodedUrlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: encodedUrlString) else {
// You could display an error message to the user from here
return
}

Unrelated to your question, but you shouldn't be using JSONSerialization to decode the JSON response. Use Codable instead.

String to Byte Conversion and Back Again Not Returning Same Result (ASCII)

ã is not an ASCII character, so how it is handled is given by the implementation

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#getBytes-java.nio.charset.Charset-

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array.

For this charset it comes out as '?'

How can I get hash from string and then string from hash back in javascript

You can't. A hash is a one-way function. 560,000 bits cannot be converted into 32 bits and back again.

How to convert an integer to the shortest url-safe string in Python?

This answer is similar in spirit to Douglas Leeder's, with the following changes:

  • It doesn't use actual Base64, so there's no padding characters
  • Instead of converting the number first to a byte-string (base 256), it converts it directly to base 64, which has the advantage of letting you represent negative numbers using a sign character.

    import string
    ALPHABET = string.ascii_uppercase + string.ascii_lowercase + \
    string.digits + '-_'
    ALPHABET_REVERSE = dict((c, i) for (i, c) in enumerate(ALPHABET))
    BASE = len(ALPHABET)
    SIGN_CHARACTER = '$'

    def num_encode(n):
    if n < 0:
    return SIGN_CHARACTER + num_encode(-n)
    s = []
    while True:
    n, r = divmod(n, BASE)
    s.append(ALPHABET[r])
    if n == 0: break
    return ''.join(reversed(s))

    def num_decode(s):
    if s[0] == SIGN_CHARACTER:
    return -num_decode(s[1:])
    n = 0
    for c in s:
    n = n * BASE + ALPHABET_REVERSE[c]
    return n

    >>> num_encode(0)
'A'
>>> num_encode(64)
'BA'
>>> num_encode(-(64**5-1))
'$_____'

A few side notes:

  • You could (marginally) increase the human-readibility of the base-64 numbers by putting string.digits first in the alphabet (and making the sign character '-'); I chose the order that I did based on Python's urlsafe_b64encode.
  • If you're encoding a lot of negative numbers, you could increase the efficiency by using a sign bit or one's/two's complement instead of a sign character.
  • You should be able to easily adapt this code to different bases by changing the alphabet, either to restrict it to only alphanumeric characters or to add additional "URL-safe" characters.
  • I would recommend against using a representation other than base 10 in URIs in most cases—it adds complexity and makes debugging harder without significant savings compared to the overhead of HTTP—unless you're going for something TinyURL-esque.


Related Topics



Leave a reply



Submit