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)
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
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)
}
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")
Converting String of ASCII character values to actual string in swift 2 (latest beta)
Your charArray
is an array of String
, and your stringArray
is
an array of Character
, so there seems to be something mixed-up.
Anyway, you can use the array of characters, slightly simplified to
let charArray = asciiArray.map { Character(UnicodeScalar(UInt32($0)!)) }
and then create a string from that array:
let string = String(charArray)
print(string)
// -86,-44,2191,14
Swift How to get an ASCII value from a character
You can't get it from the String but from a Character in a string, for instance
str.first?.asciiValue
Related Topics
How to Compare Two Dates (Nsdate) in Swift 3 and Get The Days Between Them
Convert Single File to Swift 3 in Xcode 8
Notification in Swift Returning Userinfo in Dictionary
Parametrized Unit Tests in Swift
Get Png Representation of Nsimage in Swift
Remove from Array of Anycancellable When Publisher Finishes
Swift Override Static Method Compile Error
Swiftui Scrollview Only Scroll in One Direction
Access Class Property from Instance
Is There a Method to Check The UIcollectionview Item Drag Cancellation If The Item Wasn't Moved
Missing Required Module Firebase - Jenkins Build Error
How to Get .Adjustsfontsizetofitwidth to Function Properly
iOS 12 Wkwebview Not Working with Redirects
Ambiguous Use of 'Filter' When Converting Project to Swift 4
Is There Any Reasonable Way to Access The Contents of a Characterset