我正在使用cgo
来调用动态库中一个签名如下的函数:
int decompress(int, const uint8_t *, size_t, uint8_t *, size_t);
这是我的转到代码:
// #include statements here
import "C"
import (
"unsafe"
)
func Decompress(comp_type int, data string, expected_size int) []byte {
compressedData := C.CString(data)
defer C.free(unsafe.Pointer(compressedData))
compressedDataSize := C.ulong(len(data))
decompressedData := C.malloc(C.ulong(C.sizeof_char * expected_size))
defer C.free(unsafe.Pointer(decompressedData))
decompressedDataSize := C.ulong(expected_size)
ret_val := C.XpressDecompress(C.int(comp_type),
(*C.uchar) (compressedData),
compressedDataSize,
(*C.uchar) (decompressedData),
&decompressedDataSize)
decompressed := C.GoBytes(decompressedData, C.int(decompressedDataSize))
return decompressed
}
但是,尝试构建时出现此错误:
cannot convert compressedData (type *_Ctype_char) to type *_Ctype_uchar
将输入字符串转换为const unsigned char*
的最佳方法是什么?
答案 0 :(得分:0)
以下工作有效:
compressedData := unsafe.Pointer(data)
和
(*C.uchar) (compressedData)