Saltar al contenido

Convierte entre decimal, binario y hexadecimal en Swift

Te damos la bienvenida a nuestra web, en este lugar vas a hallar la resolución de lo que andabas buscando.

Solución:

Ambos String y Int tienen inicializadores que toman un radix (base). Combinando esos, puede lograr todas las conversiones:

// Decimal to binary
let d1 = 21
let b1 = String(d1, radix: 2)
print(b1) // "10101"

// Binary to decimal
let b2 = "10110"
let d2 = Int(b2, radix: 2)!
print(d2) // 22

// Decimal to hexadecimal
let d3 = 61
let h1 = String(d3, radix: 16)
print(h1) // "3d"

// Hexadecimal to decimal
let h2 = "a3"
let d4 = Int(h2, radix: 16)!
print(d4) // 163

// Binary to hexadecimal
let b3 = "10101011"
let h3 = String(Int(b3, radix: 2)!, radix: 16)
print(h3) // "ab"

// Hexadecimal to binary
let h4 = "face"
let b4 = String(Int(h4, radix: 16)!, radix: 2)
print(b4) // "1111101011001110"

Un montón de Int y String se utiliza para convertir diferentes sistemas.

Int se utiliza para trabajar con decimal
String se utiliza para otros tipos

Es seguro convertir un string representación del sistema a decimal y decimal a string representación

String -> Int -> String

Hay muestras de

binary(2) - 0b
octal(8) - 0o
decimal(10)
hexadecimal(16) - 0x
extension Int 

    //From Decimal
    //10 -> 2
    func decToBinString() -> String 
        let result = createString(radix: 2)
        return result
    

    //10 -> 8
    func decToOctString() -> String 
//        let result = decToOctStringFormat()
        let result = createString(radix: 8)

        return result
    

    //10 -> 16
    func decToHexString() -> String 
//        let result = decToHexStringFormat()
        let result = createString(radix: 16)
        return result
    

    //10 -> 8
    func decToOctStringFormat(minLength: Int = 0) -> String 

        return createString(minLength: minLength, system: "O")
    

    //10 -> 16
    func decToHexStringFormat(minLength: Int = 0) -> String 

        return createString(minLength: minLength, system: "X")
    

    fileprivate  func createString(radix: Int) -> String 
        return String(self, radix: radix, uppercase: true)
    

    fileprivate func createString(minLength: Int = 0, system: String) -> String 
        //0 - fill empty space by 0
        //minLength - min count of chars
        //system - system number
        return String(format:"%0(minLength)(system)", self)
    

extension String 

    //To Decimal
    //2 -> 10
    func binToDec() -> Int 
        return createInt(radix: 2)
    

    //8 -> 10
    func octToDec() -> Int 
        return createInt(radix: 8)
    

    //16 -> 10
    func hexToDec() -> Int 
        return createInt(radix: 16)
    

    //Others
    //2 -> 8
    func binToOct() -> String 
        return self.binToDec().decToOctString()
    

    //2 -> 16
    func binToHex() -> String 
        return self.binToDec().decToHexString()
    

    //8 -> 16
    func octToHex() -> String 
        return self.octToDec().decToHexString()
    

    //16 -> 8
    func hexToOct() -> String 
        return self.hexToDec().decToOctString()
    

    //16 -> 2
    func hexToBin() -> String 
        return self.hexToDec().decToBinString()
    

    //8 -> 2
    func octToBin() -> String 
        return self.octToDec().decToBinString()
    

    //Additional
    //16 -> 2
    func hexToBinStringFormat(minLength: Int = 0) -> String 

        return hexToBin().pad(minLength: minLength)
    

    fileprivate func pad(minLength: Int) -> String 
        let padCount = minLength - self.count

        guard padCount > 0 else 
            return self
        

        return String(repeating: "0", count: padCount) + self
     

    fileprivate func createInt(radix: Int) -> Int 
        return Int(self, radix: radix)!
    


func testConverter() 
    let decInt = 13511
    let binStr = "11010011000111"
    let octStr = "32307"
    let hexStr = "34C7"

    //From Decimal
    //10 -> 2
    XCTAssertEqual(binStr, decInt.decToBinString())
    //10 -> 8
    XCTAssertEqual(octStr, decInt.decToOctString())
    //10 -> 16
    XCTAssertEqual(hexStr, decInt.decToHexString())

    //To Decimal
    //2 -> 10
    XCTAssertEqual(decInt, binStr.binToDec())
    //8 -> 10
    XCTAssertEqual(decInt, octStr.octToDec())
    //16 -> 10
    XCTAssertEqual(decInt, hexStr.hexToDec())

    //Others
    //2 -> 8
    XCTAssertEqual(octStr, binStr.binToOct())
    //2 -> 16
    XCTAssertEqual(hexStr, binStr.binToHex())
    //8 -> 16
    XCTAssertEqual(hexStr, octStr.octToHex())
    //16 -> 8
    XCTAssertEqual(octStr, hexStr.hexToOct())
    //16 -> 2
    XCTAssertEqual(binStr, hexStr.hexToBin())
    //8 -> 2
    XCTAssertEqual(binStr, octStr.octToBin())

    //String format feature
    //10 -> 8
    XCTAssertEqual("017", (15).decToOctStringFormat(minLength: 3))
    //10 -> 16
    XCTAssertEqual("00F", (15).decToHexStringFormat(minLength: 3))
    //16 > 2
    XCTAssertEqual("0011010011000111", hexStr.hexToBinStringFormat(minLength: 16))


Reseñas y calificaciones

Nos puedes ayudar nuestra ocupación ejecutando un comentario o valorándolo te damos la bienvenida.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *