Advertisement
SEEEEEAAAAAA10000000

Print Oktet In Decimal

Dec 6th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.55 KB | None | 0 0
  1. import Darwin
  2.  
  3. public func printOktetInDecimal<C: BidirectionalCollection>(c: C) {
  4.     let r = c.reversed()
  5.     var result = 0
  6.     for (index, item) in r.enumerated() {
  7.         guard let d = Int("\(item)") else { print("error2"); return }
  8.         guard (0...1).contains(d) else { print("error3"); return }
  9.         let rank = Int(pow(2, Double(index)))
  10.         result += rank * d
  11.     }
  12.     print("oktet: \(c), result: \(result)")
  13. }
  14.  
  15. public func testOktet() {
  16.     let z = "11110000"
  17.     printOktetInDecimal(c: z) // oktet: 11110000, result: 240
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement