duanna3634 2013-02-12 13:20
浏览 220
已采纳

如何在Go中将[] [] byte转换为** char

I would like to convert from a go [][]byte to a C **char. In other words, I have a byte matrix in go that I would like to convert to a char double pointer in C.

Please assume that I HAVE to have a [][]byte as input and a **char as output.

I know it is possible to convert from []byte to *char by doing something like:

((*C.char)(unsafe.Pointer(&data[0])))

But it does not seem possible to extend this case into the second dimension. I have tried something pretty elaborate, where I pack a [][]byte into a new []byte. I then send that []byte to a C function that creates a **char using pointer arithmetic to point into the new []byte at the correct locations.

This conversion is giving me strange behaviour though, where my data would be correct for a few iterations, but gets corrupted seemingly between function calls.

If anyone has any ideas, I would really appreciate it.

From the responses I see it is also important to state that I'm working with raw data and not strings. Hence the go byte type. The original data would, therefore, be corrupted if C string terminators are added. I'm just using C **char, because a char is one byte in size. That said, thanks for the responses. I was able to adapt the accepted answer for my needs.

  • 写回答

2条回答 默认 最新

  • dqan70724 2013-02-12 14:05
    关注

    This has to be done manually. You have to allocate a new **C.char type and loop through each element in the [][]byte slice to assign it to the new list. This involves offsetting the **C.char pointer by the correct size for each iteration.

    Here is an example program which does this.

    As the comments below suggest, if you intend to print the char * lists using something like printf in C, then ensure the input strings are NULL terminated. Ideally by converting them using the C.CString() function. This assumes that they are to be treated as strings though. Otherwise you may also need to supply a way to pass the length of each individual char * list to the C function.

    package main
    
    /*
    #include <stdlib.h>
    #include <stdio.h>
    
    void test(char **list, size_t len)
    {
        size_t i;
    
        for (i = 0; i < len; i++) {
            //printf("%ld: %s
    ", i, list[i]);
        }
    }
    */
    import "C"
    import "unsafe"
    
    func main() {
        list := [][]byte{
            []byte("foo"),
            []byte("bar"),
            []byte("baz"),
        }
    
        test(list)
    }
    
    func test(list [][]byte) {
        // Determine the size of a pointer on the current system.
        var b *C.char
        ptrSize := unsafe.Sizeof(b)
    
        // Allocate the char** list.
        ptr := C.malloc(C.size_t(len(list)) * C.size_t(ptrSize))
        defer C.free(ptr)
    
        // Assign each byte slice to its appropriate offset.
        for i := 0; i < len(list); i++ {
            element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*ptrSize))
            *element = (*C.char)(unsafe.Pointer(&list[i][0]))
        }
    
        // Call our C function.
        C.test((**C.char)(ptr), C.size_t(len(list)))
    }
    

    The output is as follows:

    $ go run charlist.go 
    0: foo
    1: bar
    2: baz
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧