How can I get unicode number of a character? [duplicate] - swift

This question already has answers here:
How to convert an Int to Hex String in Swift
(6 answers)
Closed 6 years ago.
How can I get the Unicode number of a given string ?
For example, Cyrillic Small Letter Er U+0440 stands for "р"
How can I get "U+0440" or "0440"?
====Technical information====
Unicode number: U+0440
HTML-code: &#1088
var myString = "р"
for scalar in myString.unicodeScalars {
print("\(scalar.value) ") // print: 1008
}

440 in hex is equal to 1088 in decimal:
var myString = "р"
for scalar in myString.unicodeScalars {
print(String(scalar.value, radix: 16))
}

Related

I want to get Int value range 0-25 from a-z in Swift. And also reversely [duplicate]

This question already has answers here:
How do I cycle through the entire alphabet with Swift while assigning values?
(5 answers)
Closed 1 year ago.
I want to convert alphabets to numbers, in this way: a=0, b=1, c=2 ... z=25 in Swift.
I have an array of integers range 0-25. I want to get alphabets from the Int array.
If I have an array of characters, how can I get an array of Int?
//Create an array of UInt8 vaues:
var array = [UInt8]()
for _ in 1...20 {
array.append(UInt8.random(in: 0...25))
}
//Now map the array of values to characters 'a' to 'z'
let charArray = array.map {UnicodeScalar($0 + (Character("a").asciiValue ?? 0))}
charArray.forEach { print($0) }
//Now map the char array back to int values
let valueOfA = Character("a").asciiValue ?? 0
let charToUIntArray = charArray.map { (Character($0).asciiValue ?? 0) - valueOfA}
How to get string from ASCII code in Swift?
You can make your numbers match the right characters if you add an offset to your numbers and assign this number with the Character initializer to your character.
func getChar(number: Int)->Character{
return Character(UniCodeScalar(number+97))
}
The other way around you can use the asciiValue property.
(What's the simplest way to convert from a single character String to an ASCII value in Swift?)
After that you can loop through the array and use for example functions to convert.

Building a string by repeating a single character [duplicate]

This question already has answers here:
Repeating String in Swift
(8 answers)
Closed 5 months ago.
To size a view, I needed to build a string by repeating a single character. I came up with the following:
var str = ""
for _ in 0 ..< length {
str.append("W")
}
I also came up with a functional alternative:
let str = (0 ..< digits).reduce("") { (result, _) -> String in
result + "0" // assuming 0 is the most wide number
}
Both feel a bit verbose. Is there a shorter way or a built-in function in Swift?
Use init(repeating:count:)
var str = String(repeating: "w", count: length)

Convert a String of hex to an Int [duplicate]

This question already has answers here:
Swift native functions to have numbers as hex strings
(4 answers)
Closed 6 years ago.
I'm working in Swift and I have this string as input: "0x13123ac"
And I want an Int as output: 0x13123ac
How can I do that?
Your question makes no sense. Computers store numbers in binary, not hex. Perhaps what you wanted was to convert a hex string to an integer and convert it back to hex for display purposes:
let input = "0x13123ac"
// Int(_, radix: ) can't deal with the '0x' prefix. NSScanner can handle hex
// with or without the '0x' prefix
let scanner = Scanner(string: input)
var value: UInt64 = 0
if scanner.scanHexInt64(&value) {
print("Decimal: \(value)")
print("Hex: 0x\(String(value, radix: 16))")
}

How to parse a String like "14px" to get Int value 14 in Swift [duplicate]

This question already has answers here:
Swift How to get integer from string and convert it into integer
(10 answers)
Closed 6 years ago.
I receive from a server API a String like "14px". How to transform this String to Int with value 14 ignoring substring "px"?
If string always come with just px at last you can subscript it and ignore the last 2 characters.
let str = "14px"
var numStr = str.substring(to: str.index(name.endIndex, offsetBy: -2))
print(numStr) // "14"
//Now you can convert "14" to Int
print(Int(numStr))
Or
print(Int(String(str.characters.dropLast(2))))

Convert decimal to binary in matlab [duplicate]

This question already has answers here:
Convert Decimal to Binary Vector
(4 answers)
Closed 7 years ago.
If I use a decimal to binary command in Matlab how can I get the result in four digits? e.g. 7 =0111 or 2=0010.
Thanks in advance
Othman
Use dec2bin:
str = dec2bin(7) // 0111
str = dec2bin(7,8) // 00000111, pad the result to 8 bits