首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将*_Ctype_char转换为*_Ctype_uchar

如何将*_Ctype_char转换为*_Ctype_uchar
EN

Stack Overflow用户
提问于 2019-07-10 02:34:10
回答 1查看 1.8K关注 0票数 2

我使用cgo调用动态库中的一个函数,其签名如下所示:

代码语言:javascript
复制
int decompress(int, const uint8_t *, size_t, uint8_t *, size_t);

下面是我的go代码:

代码语言:javascript
复制
// #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*的最佳方法是什么

EN

回答 1

Stack Overflow用户

发布于 2019-07-10 03:08:42

以下方法起作用了:

代码语言:javascript
复制
compressedData := unsafe.Pointer(data)

代码语言:javascript
复制
(*C.uchar) (compressedData)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56958728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档