如何将“2016-11-06 21:09:55”转换为[07,e0,11,06,21,09,55] in swift

时间:2016-11-06 09:19:16

标签: swift hex

BLE在数据传输中,需要把时间转换成7个字节,2016年转换为十六进制其他不变,如何将“2016”转换为[0x07,0xe0]

1 个答案:

答案 0 :(得分:0)

2016以十六进制变为0x7E0,因此您只需要分割较高字节和较低字节。似乎是C语言的第一步练习。

func getYearBytes(year: Int) -> [UInt8] {
    let yearByte0 = UInt8(year>>8)
    let yearByte1 = UInt8(year & 0xFF)
    return [yearByte0, yearByte1]
}
print(getYearBytes(year: 2016)) //-> [7, 224] (in decimal) ([0x07, 0xE0] in hexadecimal)