How in swift to convert Int16 to two UInt8 Bytes

前端 未结 4 1604
甜味超标
甜味超标 2021-02-04 07:42

I have some binary data that encodes a two byte value as a signed integer.

bytes[1] = 255  // 0xFF
bytes[2] = 251  // 0xF1

Decoding

4条回答
  •  萌比男神i
    2021-02-04 08:17

    I would just do this:

    let a = UInt8(nv >> 8 & 0x00ff)  // 255
    let b = UInt8(nv & 0x00ff)       // 241
    

提交回复
热议问题