1

Let say we can mark weekend day active or inactive. I need to use Integer to say system that I marked day active or inactive, to retrieve this integer I need to use array [1, 1, 1, 1, 1, 1, 1]. So if you see at this array all day of week marked as active and in Hexadecimal it's 0000007F.

If I use [0, 0, 0, 0, 0, 0, 1] this string it means Hexadecimal = 00000001. So my question is how to create Hexadecimal from array and then get form it an Integer. So in case with 0000007F it should be 127.

I assume that it should be something like that:

let array = [1, 1, 1, 1, 1, 1, 1] let hexadecimal = array.toHexadecimal let intNumber = hexadecimal.toInt

print(intNumber) // prints 127

Also I guess it can be for example an array with ints like [0, 1, 1, 1, 1, 0, 1] wich means that Monday and from Wednesday till Saturday (including) are active days.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Swift has bit shift operators. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AdvancedOperators.html – john elemans Sep 12 '16 at 16:02
  • @johnelemans, thanks for comment, but for example how to build it. For example if I have an array of [0, 1, 0, 1 ... ] how to get right value `let bits: UInt8 = ` *0b11111100* ? I don't know how to transform it. – Matrosov Oleksandr Sep 12 '16 at 16:10
  • http://stackoverflow.com/a/27191772/2303865 – Leo Dabus Sep 12 '16 at 16:17
  • @LeoDabus, thanks for comment. it's ok if you know how to get `F00`, but I don't know how to do it, I know that I have array of checked object and I need to get an Int. My system does not know what is F or B, it's just a set of 0101010 which is active or not. remove string from question to get rid confusion. – Matrosov Oleksandr Sep 12 '16 at 16:25
  • @johnelemans removed string from question to get rid of confusion. – Matrosov Oleksandr Sep 12 '16 at 16:29
  • examples bits = bits << 1 moves all the bits to the left by 1. bits = bits | 1 will set bit 1 to one and leave the others as is. – john elemans Sep 12 '16 at 16:32
  • @johnelemans, thanks for comments, not sure I am cleat in my question, but for example, I am not sure what you mean leave the others, I have an array of ints `[1, 0, 0, 1, 0, 1, 1]` and I don't know how to get bites from it. – Matrosov Oleksandr Sep 12 '16 at 16:39
  • try `Int([1, 1, 1, 1, 1, 1, 1].map{String($0)}.joined(), radix: 2)` will return 127 – Leo Dabus Sep 12 '16 at 16:48
  • @LeoDabus, thanks, Value of type '[String]' has no member 'joined' – Matrosov Oleksandr Sep 12 '16 at 16:53
  • update your Xcode. Xcode 8 GM is already available for developers – Leo Dabus Sep 12 '16 at 16:54
  • 1
    `[1, 1, 1, 1, 1, 1, 1].reduce(0, combine: {$0*2 + $1})` would also work and probably faster than map – Leo Dabus Sep 12 '16 at 16:54
  • `String([1, 1, 1, 1, 1, 1, 1].reduce(0, combine: {$0*2 + $1}), radix: 16)` if you need the hexa string `"7f"` – Leo Dabus Sep 12 '16 at 16:59
  • @LeoDabus nice! sounds like a magic!!!!!0 thank you – Matrosov Oleksandr Sep 12 '16 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123176/discussion-between-matrosov-alexander-and-leo-dabus). – Matrosov Oleksandr Sep 12 '16 at 17:10

1 Answers1

2

You can use reduce method to sum up your binary array (inspired at this answer) and use String(radix:) initializer to convert your integer to hexa string:

Swift 3 • Xcode 8 or later

let binaryArray =  [1, 1, 1, 1, 1, 1, 1]

let integerValue = binaryArray.reduce(0, {$0*2 + $1})
let hexaString = String(integerValue, radix: 16)  // "7f"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571