duandianzhong8315 2019-07-09 18:34
浏览 751

如何将* _Ctype_char转换为* _Ctype_uchar

I'm using cgo to call a function in a dynamic library whose signature looks like this:

int decompress(int, const uint8_t *, size_t, uint8_t *, size_t);

Here is my go code:

// #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
}

However, I'm getting this error when I try to build: cannot convert compressedData (type *_Ctype_char) to type *_Ctype_uchar

What is the best way for me to convert the input string into a const unsigned char*?

  • 写回答

1条回答 默认 最新

  • 普通网友 2019-07-09 19:08
    关注

    The following worked:

    compressedData := unsafe.Pointer(data)
    

    and

    (*C.uchar) (compressedData)
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题