elichai2
elichai2

Reputation: 1405

cgo: Converting `[]uchar` to `[]byte`

First of all, cgo is pretty cool, thanks for that :)

I do have a question though, I have a lot of C structs that just contain a byte array, and I want to use the array in it as a []byte. ie:

typedef struct {
    unsigned char data[64];
} pubkey;

I would like to be able to then do:

type SchnorrPublicKey struct {
    pubkey C.pubkey
}

func (key *SchnorrPublicKey) String() string {
    return hex.EncodeToString(key.pubkey.data[:])
}

But I get the following error: ./schnorr.go:39:43: cannot use key.pubkey.data[:] (type []_Ctype_uchar) as type []byte in argument to hex.EncodeToString I can get this working by doing something like: return hex.EncodeToString(C.GoBytes(unsafe.Pointer(&key.pubkey.data[0]), C.int(len(key.pubkey.data))))

But it feels kinda stupid, it's a lot of complexity to convert an already go slice into another go slice just to replace the _Ctype_uchar type with byte type, and they are 100% compatible anyway.

Is there a better way to do this?

Upvotes: 1

Views: 1320

Answers (1)

bcmills
bcmills

Reputation: 5197

There is an open proposal for a function to do this in https://golang.org/issue/19367.

In the meantime, if you have a constant upper bound on the size of the slice (as, in this case, 64):

    var cPtr = &key.pubkey.data[0]
    const upperBound = 64
    var n = 64

    slice := (*[upperBound]byte)(unsafe.Pointer(cPtr))[:n:n]

and if you don't, see the function in https://github.com/golang/go/issues/13656#issuecomment-303216308.

Upvotes: 2

Related Questions