後續更新見個人部落格頁
[uint8]
是swift中的陣列型別,也是作用比較特殊的一種陣列型別, 常用於底層互動與記憶體操作與管理。如: 字串編譯碼, 充當位元組緩衝區等等
string有對應的直接從[uint8]
或[int8]
轉換成string的建構函式
/// "這是乙個字串" 的utf8編碼
let bytes:
[uint8]=
[232
,191
,153
,230
,152
,175
,228
,184
,128
,228
,184
,170
,229
,173
,151
,231
,172
,166
,228
,184
,178
]/// 轉換成字串
iflet msg =
string
(bytes: bytes, encoding:
.utf8)
string
轉[uint8]
或[int8]
時需要借助中間角色data
let msg =
"這是乙個字串"
iflet data = msg.
data
(using:
.utf8)
這裡的轉換指的是使用同一塊記憶體位址
c語言指標型別
swift語言指標物件型別
char *
unsafemutablepointer
const char *
unsafepointer
unsigned char *
unsafemutablepointer
const unsigned char *
unsafepointer
void *
unsafemutablerawpointer
const void *
unsaferawpointer
這些帶有buffer的指標型別, 可以理解為對應的不帶buffer的指標型別加上了緩衝區大小, 比如:
unsafepointer對應 const int *, 表示僅有位址那unsafebufferpointer對應 const int * 加 size, 表示該位址與記憶體大小所對應的一塊緩衝區
[uint8]
->unsaferawbufferpointer
->unsafebufferpointer
->unsafepointer
(int8同理)
/// 0. 原始位元組資料 8位元組
let buffer =
[uint8
](repeating:0,
count:8
)/// 1. [uint8] -> unsaferawbufferpointer
let unsaferawbufferpointer = buffer.withunsafebytes
/// 2. unsaferawbufferpointer -> unsafebufferpointer
let unsafebufferpointer = unsaferawbufferpointer.
bindmemory
(to:
uint8
.self
)/// 3. unsafebufferpointer-> unsafepointer
let unsafepointer = unsafebufferpointer.baseaddress
其中步驟1
和步驟2
可以合併
/// 0. 原始位元組資料 8位元組
let buffer =
[uint8
](repeating:0,
count:8
)/// 1. [uint8] -> unsafebufferpointer
let unsafebufferpointer = buffer.withunsafebufferpointer
/// 2.. unsafebufferpointer-> unsafepointer
let unsafepointer = unsafebufferpointer.baseaddress
[uint8]
->unsafemutablerawbufferpointer
->unsafemutablebufferpointer
->unsafemutablepointer
(int8同理)
/// 0. 原始位元組資料 8位元組
var buffer =
[uint8
](repeating:0,
count:8
)/// 1. [uint8] -> unsafemutablerawbufferpointer
let unsafemutablerawbufferpointer = buffer.withunsafemutablebytes
/// 2. unsafemutablerawbufferpointer -> unsafemutablebufferpointer
let unsafemutablebufferpointer = unsafemutablerawbufferpointer.
bindmemory
(to:
uint8
.self
)/// 3. unsafemutablebufferpointer-> unsafemutablepointer
let unsafemutablepointer = unsafemutablebufferpointer.baseaddress
其中的步驟1和步驟2也是可以合併的
/// [uint8] -> unsafemutablebufferpointer
let unsafemutablebufferpointer = buffer.withunsafemutablebufferpointer
在上述的步驟裡, 在步驟1
得到的unsafemutablerawbufferpointer
就可以通過該物件的baseaddress
欄位就能獲取到對應的void *
如:
/// 獲取 const void *
let unsaferawpointer = unsaferawbufferpointer.baseaddress
/// 獲取void *
let unsafemutablerawpointer = unsafemutablerawbufferpointer.baseaddress
/// 這裡是示例, 型別為 unsafemutablepointer, 也就是 char *
/// message可能來自任何c語言的介面
let message =
strerror
(errno)
/// 通過指標和大小來構造 unsafebufferpointer
let unsafebufferpointer =
unsafebufferpointer
<
int8
>
(start: message,
count
:strlen
(msg!))
/// 構造data
let data =
data
(buffer: unsafebufferpointer)
但是如果需要拷貝出來乙份 [uint8]或者[int8],可以使用map函式
/// 緩衝區裡的資料
let data =
data
(buffer: unsafebufferpointer)
/// [uint8]
let u8a = data.
map/// [int8]
let s8a = data.
map
let text =
string
(data: data, encoding:
.utf8)
出現u8和uint8之間不匹配的解決辦法
struct si4438 frame uint8 head 5月4號修改uint8變為u8 uint8 dev id 4 介面卡id uint8 my id 2 子裝置id uint8 handle 報文上一跳 uint8 ctrl uint8 data 上電後廣播的是自己的裝置可控燈泡數 uin...
golang中uint8位元組切片轉字串
假如拿到了乙個位元組切片test1 package main import fmt reflect func main fmt.println test1 test2 string test1 fmt.println test2 fmt.println reflect.typeof test1 0 ...
swift學習筆記(8) 列舉
enum someenumeration 下面是用列舉表示指南針四個方向的例子 enum compasspoint 列舉中定義的值 如 north,south,east和west 是這個列舉的成員值 或成員 你可以使用case關鍵字來定義乙個新的列舉成員值。注意 與 c 和 objective c ...