6

I can't seem to figure out how to do this even though I've searched through documentation.

I'm trying to figure out how to convert a character at an index in an array to an integer.

For example, say I have a character array named "container", I can't figure out how to do:

var number:Integer = container[3]

Thanks for the help!

4
  • Do you mean the Swift type Character or CChar/CString? Commented Jun 28, 2014 at 5:43
  • I think you can call utf8 on the array and then find the corresponding index. I had this same problem when converting a string to a double. Commented Jun 28, 2014 at 5:46
  • its type Array<Character> Commented Jun 28, 2014 at 5:50
  • Let's say the third element of the array is the character 🐶. What do you want number to end up as? Commented Jun 28, 2014 at 9:08

6 Answers 6

9

Swift doesn't make it easy to convert between primitive and typed representations of things. Here's an extension that should help in the meantime:

extension Character {
    func utf8Value() -> UInt8 {
        for s in String(self).utf8 {
            return s
        }
        return 0
    }

    func utf16Value() -> UInt16 {
        for s in String(self).utf16 {
            return s
        }
        return 0
    }

    func unicodeValue() -> UInt32 {
        for s in String(self).unicodeScalars {
            return s.value
        }
        return 0
    }
}

This allows you to get pretty close to what you want:

let container : Array<Character> = [ "a", "b", "c", "d" ]
/// can't call anything here, subscripting's also broken
let number = container[2]
number.unicodeValue() /// Prints "100"

For any engineers that come across this question, see rdar://17494834

Sign up to request clarification or add additional context in comments.

Comments

5

I am not sure that it is effective or not but at least it worked. I converted Character to String then to Int.

String(yourCharacterInArray).toInt()

Comments

4

You may try this:

var container = "$0123456789"
var number:Int = Array(container.utf8).map { Int($0) }[3]

It's totally ugly, but it does the job. Also it is a bit computational expensive (O(n) each time one access a character in a string). Still this can be a trick to get back a way to build the CStrings:

typealias CString = Array<CChar>
func toCString(string: String) -> CString {
    return Array(string.utf8).map { CChar($0) } + [0]
}
var cString = toCString("$ 0123456789")
println("The 2nd character in cString has value \(cString[1])") // It outputs 32

or without implementing a function:

var container = "$ 0123456789"
var containerAsCString = Array(container.utf8).map { CChar($0) } + [0]
println("The 2nd character in container has value \(containerAsCString[1])") // It outputs 32

Comments

0

Why not just convert the character to String, get the unicodeScalars for it and extract the .value on the scalar?

something like:

var chr: [Character] = ["C", "B", "A"]

for a in String(chr[1]).unicodeScalars {
    println(a.value)}

Comments

0

For me worked something like:

"\(container[3])".toInt()

Comments

-1

Why not just for loop the array and convert everything to Int?

https://developer.apple.com/Library/mac/documentation/General/Reference/SwiftStandardLibraryReference/index.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.