Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert from [][]byte to **char in go

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.

like image 584
John Gilmore Avatar asked Feb 12 '13 13:02

John Gilmore


People also ask

Can we convert byte to char?

First, the byte is converted to an int via widening primitive conversion (§5.1. 2), and then the resulting int is converted to a char by narrowing primitive conversion (§5.1. 3).

How do you change type in Golang?

Go is a statically typed language, which means types of variables must be known at compile time, and you can't change their type at runtime.

How do you cast a byte to a string in Golang?

Convert byte array to string using bytes package We can use the bytes package NewBuffer() function to create a new Buffer and then use the String() method to get the string output.

What is a [] byte in Golang?

The byte type in Golang is an alias for the unsigned integer 8 type ( uint8 ). The byte type is only used to semantically distinguish between an unsigned integer 8 and a byte. The range of a byte is 0 to 255 (same as uint8 ).


1 Answers

Untested skeleton:

func foo(b [][]byte) {
        outer := make([]*C.char, len(b)+1)
        for i, inner := range b {
                outer[i] = C.CString(string(inner))
        }
        C.bar(unsafe.Pointer(&outer[0])) // void bar(**char) {...}
}

EDIT: Full example (tested):

package main

/*
#include <stdlib.h>
#include <stdio.h>

void bar(char **a) {
        char *s        ;
        for (;(s = *a++);)
                printf("\"%s\"\n", s);
}
*/
import "C"
import "unsafe"

func foo(b [][]byte) {
        outer := make([]*C.char, len(b)+1)
        for i, inner := range b {
                outer[i] = C.CString(string(inner))
        }
        C.bar((**C.char)(unsafe.Pointer(&outer[0]))) // void bar(**char) {...}
}

func main() {
        foo([][]byte{[]byte("Hello"), []byte("world")})
}

(15:24) jnml@fsc-r550:~/src/tmp/SO/14833531$ go run main.go 
"Hello"
"world"
(15:25) jnml@fsc-r550:~/src/tmp/SO/14833531$ 
like image 78
zzzz Avatar answered Sep 21 '22 19:09

zzzz