0

I am working on some bluetooth project which sends the value in the form of 8 bit data from two characteristics i.e 8 bit data from one characteristic uuid and one more 8 bit data from another characteristic uuid.

I need to consider first 8 bit data is most significant byte and second 8 bit data is least significant byte.

Now i need to do combine them into a 16 bit value and display the result.

I am new to bit and bytes conversion in swift.

Please help me out.

Thanks in advance.

  • 1
    Possible duplicate of [Convert a two byte UInt8 array to a UInt16 in Swift](https://stackoverflow.com/questions/25267089/convert-a-two-byte-uint8-array-to-a-uint16-in-swift) – Martin R Jul 31 '17 at 07:08

2 Answers2

2
let mostSignificantByte: UInt8 = 0x01
let leastSignificantByte: UInt8 = 0x02

let twoByteInteger = (UInt16(mostSignificantByte) << 8) | UInt16(leastSignificantByte)

// twoByteInteger is 0x0102
Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • Thanks. Here you mentioned mostSignificantByte: UInt8 = 0x01 means 1 and leastSignificantByte:UInt8 = 0x02 means 2 and the resultant value is 0x0102 means 258. please can you tell me how it works. – Manoj Kumar M Jul 31 '17 at 04:58
  • The 0x01 and 0x02 are placeholder values; replace those with whatever your eight-bit values are. As for what the code is doing: UInt16(x) just converts the 8 bit integer to a 16 bit integer. So 0x01 becomes 0x0001. << is the left bitshift operator. It "pushes" the value some number of bits to the left. In this case, I shifted it 8 bits to the left, so 0x0001 becomes 0x0100. And |, of course, ORs two values together. So, 0x0100 | 0x0002 == 0x0102. Thus, I have combined the two 8-bit integers into one 16-bit integer. – Charles Srstka Jul 31 '17 at 05:15
  • Sorry I am getting the value in the form of [UInt8] format. If I try to use the above equation i am getting an error. Please help me out. – Manoj Kumar M Jul 31 '17 at 05:30
  • I assume you mean that the array has two elements in it? In that case, you can just: let foo = (UInt16(array[0]) << 8) | UInt16(array[1]) – Charles Srstka Aug 01 '17 at 06:05
1

Swift 3X and Xcode 8.0

  let byte1: UInt8 = 0x01
    let byte2: UInt8 = 0x02
    let bit16 = UnsafePointer([byte1,byte2]).withMemoryRebound(to: UInt16.self,
                                                     capacity: 1) {
                                                        $0.pointee
    }
    print(bit16) // 513

You can go here more detail

Arjun Yadav
  • 1,369
  • 1
  • 10
  • 23
  • If you think that a question has been asked and answered before then you can *flag* it as a duplicate. There is no need to repeat existing answers. – Martin R Jul 31 '17 at 07:10
  • @MartinR will you please mark Duplicate the Question. I can't have authority to mark question duplicate – Arjun Yadav Jul 31 '17 at 07:12
  • I got value from one characteristic like this [1,0,0,0] i.e [UInt8] but in peripheral I am sending 0x01 same for second one 0x02 I received [2,0,0,0]. Now how can I combine these values to form a single 16 bit. – Manoj Kumar M Jul 31 '17 at 10:00
  • @ManojKumarM this method are combined two 8bit data one single 16bit – Arjun Yadav Jul 31 '17 at 10:24
  • @sara But I need to combine an array of UInt8 from two characteristics uuid. how this is possible. – Manoj Kumar M Jul 31 '17 at 10:26
  • @ManojKumarM You want a for loop to combine two UInt8 array in 16bit – Arjun Yadav Jul 31 '17 at 10:46
  • This method depends on the endian-order of the host machine. Doing it using the bit-shift operator will behave exactly the same on any architecture, so I recommend doing it that way instead. – Charles Srstka Aug 01 '17 at 06:06