Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Turn Int to binary representations

Tags:

swift

bitmask

I receive an Int from my server which I’d like to explode in to an array of bit masks. So for example, if my server gives me the number 3, we get two values, a binary 1 and a binary 2.

How do I do this in Swift?

like image 679
Adam Carter Avatar asked Sep 01 '26 13:09

Adam Carter


1 Answers

You could use:

let number = 3
//radix: 2 is binary, if you wanted hex you could do radix: 16
let str = String(number, radix: 2)
println(str)

prints "11"

let number = 79
//radix: 2 is binary, if you wanted hex you could do radix: 16
let str = String(number, radix: 16)
println(str)

prints "4f"

like image 175
Jeremy Pope Avatar answered Sep 04 '26 04:09

Jeremy Pope



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!