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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀