What's the simplest way to convert from a single character String to an ASCII value in Swift?
edit/update Swift 5.2 or later
extension StringProtocol {
var asciiValues: [UInt8] { compactMap(\.asciiValue) }
}
"abc".asciiValues // [97, 98, 99]
In Swift 5 you can use the new character properties isASCII and asciiValue
Character("a").isASCII // true
Character("a").asciiValue // 97
Character("á").isASCII // false
Character("á").asciiValue // nil
Old answer
You can create an extension:
Swift 4.2 or later
extension Character {
var isAscii: Bool {
return unicodeScalars.allSatisfy { $0.isASCII }
}
var ascii: UInt32? {
return isAscii ? unicodeScalars.first?.value : nil
}
}
extension StringProtocol {
var asciiValues: [UInt32] {
return compactMap { $0.ascii }
}
}
Character("a").isAscii // true
Character("a").ascii // 97
Character("á").isAscii // false
Character("á").ascii // nil
"abc".asciiValues // [97, 98, 99]
"abc".asciiValues[0] // 97
"abc".asciiValues[1] // 98
"abc".asciiValues[2] // 99
Getting character ASCII value as an Integer in Swift
In Swift, a Character
may not necessarily be an ASCII one. It would for example have no sense to return the ascii value of "quot; which requires a large unicode encoding. This is why asciiValue
property has an optional UInt8
value, which is annotated UInt8?
.
The simplest solution
Since you checked yourself that the character isAscii
, you can safely go for an unconditional unwrapping with !
:
var tempo:Int = Int(n.asciiValue!) // <--- just change this line
A more elegant alternative
You could also take advantage of optional binding that uses the fact that the optional is nil when there is no ascii value (i.e. n was not an ASCII character):
if let tempo = n.asciiValue // is true only if there is an ascii value
{
temp += (Int(tempo) | key)
}
Converting to Char/String from Ascii Int in Swift
It may not be as clean as Java, but you can do it like this:
var string = ""
string.append(Character(UnicodeScalar(50)))
You can also modify the syntax to look more similar if you like:
//extend Character so it can created from an int literal
extension Character: IntegerLiteralConvertible {
public static func convertFromIntegerLiteral(value: IntegerLiteralType) -> Character {
return Character(UnicodeScalar(value))
}
}
//append a character to string with += operator
func += (inout left: String, right: Character) {
left.append(right)
}
var string = ""
string += (50 as Character)
Or using dasblinkenlight's method:
func += (inout left: String, right: Int) {
left += "\(UnicodeScalar(right))"
}
var string = ""
string += 50
Swift - Convert a binary string to its ascii values
You may need to split the input binary digits into 8-bit chunks, and then convert each chunk to an ASCII character. I cannot think of a super simple way:
var binaryBits = "010010000110010101111001"
var index = binaryBits.startIndex
var result: String = ""
for _ in 0..<binaryBits.characters.count/8 {
let nextIndex = binaryBits.index(index, offsetBy: 8)
let charBits = binaryBits[index..<nextIndex]
result += String(UnicodeScalar(UInt8(charBits, radix: 2)!))
index = nextIndex
}
print(result) //->Hey
How to convert sequence of ASCII code into string in swift 4?
As already mentioned decimal ASCII values are in range of 0-255 and can be more than 2 digits
Based on Sulthan's answer and assuming there are no characters < 32 (0x20) and > 199 (0xc7) in the text this approach checks the first character of the cropped string. If it's "1" the character is represented by 3 digits otherwise 2.
func convertAscii(asciiStr: String) {
var source = asciiStr
var result = ""
while source.count >= 2 {
let digitsPerCharacter = source.hasPrefix("1") ? 3 : 2
let charBytes = source.prefix(digitsPerCharacter)
source = String(source.dropFirst(digitsPerCharacter))
let number = Int(charBytes)!
let character = UnicodeScalar(number)!
result += String(character)
}
print(result) // "Happy Birthday"
}
convertAscii(asciiStr: "7297112112121326610511411610410097121")
How to get string from ASCII code in Swift?
As a character:
let c = Character(UnicodeScalar(65))
Or as a string:
let s = String(UnicodeScalar(UInt8(65)))
Or another way as a string:
let s = "\u{41}"
(Note that the \u
escape sequence is in hexadecimal, not decimal)
Related Topics
Swift 3 Incorrect String Interpolation With Implicitly Unwrapped Optionals
Wait Until Swift For Loop With Asynchronous Network Requests Finishes Executing
Instantiated Optional Variable Shows as Nil in Xcode Debugger
How to Generate a Random Number in Swift Without Repeating the Previous Random Number
Swift Variable Decorations With "" (Question Mark) and "!" (Exclamation Mark)
What Is the In-Practice Difference Between Generic and Protocol-Typed Function Parameters
How to Apply the Type to a Nsfetchrequest Instance
How to Get Current Language Code With Swift
What Is the Meaning of the '#' Mark in Swift Language
How to Disable Pasting in a Textfield in Swift
Swipe-Able Table View Cell in iOS 9
How to Do a Swift For-In Loop With a Step
Having Hard Time Implement a Simple Singleton in Swift
How to Determine If a String Contains a Character from a Set in Swift
Swiftui 2: the Way to Open View in New Window