我使用cgo
调用动态库中的一个函数,其签名如下所示:
int decompress(int, const uint8_t *, size_t, uint8_t *, size_t);
下面是我的go代码:
// #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*
的最佳方法是什么
https://stackoverflow.com/questions/56958728
复制相似问题