Generate an Integer Binary Representation Using Swift

How to convert a decimal number to binary in Swift?

You can convert the decimal value to a human-readable binary representation using the String initializer that takes a radix parameter:

let num = 22
let str = String(num, radix: 2)
print(str) // prints "10110"

If you wanted to, you could also pad it with any number of zeroes pretty easily as well:

Swift 5

func pad(string : String, toSize: Int) -> String {
var padded = string
for _ in 0..<(toSize - string.count) {
padded = "0" + padded
}
return padded
}

let num = 22
let str = String(num, radix: 2)
print(str) // 10110
pad(string: str, toSize: 8) // 00010110

Change negative number to binary in Swift


func printBinary4(x: Int) {
let numBit = 4

var i = x
if i < 0 {
i = 0b1 << numBit + i
}

var str = String(i, radix: 2)
if str.characters.count < numBit {
str = String(repeatElement("0", count: numBit - str.characters.count)) + str
}
print(str)
}

printBinary4(x: -1)
printBinary4(x: -2)
printBinary4(x: -3)
printBinary4(x: 0)
printBinary4(x: 4)

1111
1110
1101
0000
0100

Convert a binary string into an array of integers in swift 3

A possible solution (now updated for Swift 4 and later):

let zeroOneString = "01101010101001010111"

let resultArray = zeroOneString.compactMap { char in
char == "0" ? 0 : char == "1" ? 1 : nil
}

print(resultArray) // [0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1]

zeroOneString.compactMap maps the sequences of all characters in the string.
The closure maps "0" to 0 and "1" to 1. Everything else is mapped to nil and ignored by compactMap.

Convert between Decimal, Binary and Hexadecimal in Swift

Both String and Int have initializers which take a radix (base). Combining those, you can achieve all of the conversions:

// 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"

String of Binary Values to Binary File

If you have a string with a series of “0” and “1” characters, and want a binary representation, you can do:

extension String {
var binaryData: Data {
let values = try! NSRegularExpression(pattern: "[01]{8}")
.matches(in: self, range: NSRange(startIndex..., in: self))
.compactMap { Range($0.range, in: self) }
.compactMap { self[$0] }
.compactMap { UInt8($0, radix: 2) }
return Data(values)
}
}

Thus

let input = "010000010100001001000011"
let data = input.binaryData // 0x41 0x42 0x43

You can then write that Data to a file, or do whatever you want with it.



Related Topics



Leave a reply



Submit