Convert a String of hex to an Int [duplicate] - swift

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))")
}

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.

How can I return a combined Int from an [Int] array? [duplicate]

This question already has answers here:
Concatenate Swift Array of Int to create a new Int
(5 answers)
Closed 2 years ago.
I have an [Int] array like so:
[1, 2, 3]
How can I apply a function on this so it returns:
123
?
let nums = [1, 2, 3]
let combined = nums.reduce(0) { ($0*10) + $1 }
print(combined)
Caveats
Make sure the Int won't overflow if the number gets too long (+263 on a 64-bit system).
You need to also be careful all numbers in the list aren't more than 9 (a single base-10 digit), or this arithmetic will fail. Use the String concatenation technique to ensure that all base-10 numbers are correctly handled. But, again, you need to be careful that the number won't overflow if you choose to convert it back to an Int.
We can do like below...
var finalStr = ""
[1,2,3].forEach {
finalStr.append(String($0))
}
if let number = Int(finalStr) {
print(number)
}

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)

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))))

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

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))
}