SwiftIntelHex 是 Intel 十六进制目标文件格式 (.hex) 的解析器,https://en.wikipedia.org/wiki/Intel_HEX。提取的数据例如可用于编程微控制器或 EPROM 的内存。
该库包含了处理 hex 文件的所有基本功能,例如
IntelHexFile.parseString
函数解析整个文件。文件必须以字符串形式提供,并且该函数必须在异步上下文中调用。解析错误可以作为 IntelHexFile.ParseError
抛出。
let testString =
"""
:020000021000EC
:10010000214601360121470136007EFE09D2190140
:100110002146017EB7C20001FF5F16002148011988
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF
"""
Task {
do {
let hexFile = try await IntelHexFile.parseString(testString)
} catch {
// Error handling of IntelHexFile.ParseError
print(error.localizedDescription)
}
}
IntelHexFile
对象包含有关 hex 文件中记录和提取块的所有信息。
/* the first start address in the file */
hexFile.startAddress
/* contains the concatinated data of all data records
* in the file (assuming they are all adjacent, starting at the same address)
*/
hexFile.consolidatedData
/* the start address of the first data block */
hexFile.blocks?.first?.startAddress
/* the data of the first data block */
hexFile.blocks?.first?.data
/* the second data block
* (data records are split into blocks wherever the start address
* of the next data record is not adjacent to the data of the previous record)
*/
hexFile.blocks?[1]
/* array of all records (lines) in the hex file */
hexFile.records
该库可以与 XCode 中的 Swift Package Manager 一起使用,或者手动添加为包依赖项。