1 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
我想將 uint8_t 數(shù)組從 C 發(fā)送到 Go [并且作為數(shù)組或切片]
對(duì)于你的例子,
package main
/*
#include <stdint.h>
uint8_t Plaintext[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t *send_data( ) {
return Plaintext;
}
*/
import "C"
import (
"fmt"
"math"
"unsafe"
)
func main() {
// your example
data := (*[16]byte)(unsafe.Pointer(C.send_data()))
fmt.Printf("\n%T:\n%d %d : %v\n", data, len(data), cap(data), *data)
// array example
const c = 16 // array length is constant
a := (*[c]byte)(unsafe.Pointer(C.send_data()))
fmt.Printf("\n%T:\n%d %d : %v\n", a, len(a), cap(a), *a)
// slice example
var v = 16 // slice length is variable
var s []byte
const vmax = math.MaxInt32 / unsafe.Sizeof(s[0])
s = (*[vmax]byte)(unsafe.Pointer(C.send_data()))[:v:v]
fmt.Printf("\n%T:\n%d %d : %v\n", s, len(s), cap(s), s)
}
輸出:
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
*[16]uint8:
16 16 : [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
[]uint8:
16 16 : [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
- 1 回答
- 0 關(guān)注
- 216 瀏覽
添加回答
舉報(bào)