ChairNKeyboard
ChairNKeyboard

Reputation: 23

How to write "decToBin" in Swift?

I have been looking for an equivalent to JavaScripts decToBin in Swift 2. I have not been able to find anything. I would like to replicate the following JavaScript code code:

var max = 511;
var maxlength = decToBin(max).toString().length;

Upvotes: 0

Views: 57

Answers (1)

Krodak
Krodak

Reputation: 1493

You can use String to achieve this:

let max       = 511
let maxlength = String(max, radix: 2).characters.count

You can find more in Apple String documentation

Upvotes: 2

Related Questions