Note that in general C strings are not restricted to 2³¹ bytes on 64-bit 
systems.
(Larger strings are unusual, but fairly trivial to construct.)
So I would recommend avoiding hard-coded array types of any size.

You say that C.GoBytes truncates at zero. However, that does not reproduce 
for me.
Are you sure that you are passing in the correct value for `sz`?

―

example.com$ cat main.go
package main

import (
        "fmt"
        "unsafe"
)

/*
#include <stddef.h>
#include <string.h>

char* message() {
        return "Hello,\0 Go";
}
*/
import "C"

func charToBytes(src *C.char, sz int) []byte {
        return C.GoBytes(unsafe.Pointer(src), C.int(sz))
}

func main() {
        b := charToBytes(C.message(), 10)
        fmt.Printf("%s\n", b)
}

example.com$ go run main.go
Hello, Go

example.com$
On Saturday, June 6, 2020 at 3:35:27 AM UTC-4 Tamás Gulácsi wrote:

> Use 1<<31 instead of 1024.
> And ifthe C side won't modify/free the *uchar, then you don't need the 
> copy.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0f4ec39f-9222-4c4f-8e72-4b3bf38a9de3n%40googlegroups.com.

Reply via email to