Tuesday, 15 February 2011

binary - How to convert hex number to bin in Swift? -



binary - How to convert hex number to bin in Swift? -

i have string variable: var str = "239a23f" how convert string binary number? str.toint() not work.

you can utilize nsscanner() foundation framework:

let scanner = nsscanner(string: str) var result : uint32 = 0 if scanner.scanhexint(&result) { println(result) // 37331519 }

or bsd library function strtoul()

let num = strtoul(str, nil, 16) println(num) // 37331519

as of swift 2 (xcode 7), integer types have an

public init?(_ text: string, radix: int = default)

initializer, pure swift solution available:

let str = "239a23f" allow num = int(str, radix: 16)

swift binary hex

No comments:

Post a Comment