简体   繁体   中英

Go - int16 slice to byte slice

I'm writing a Go program that interfaces with libalsa. I have my PCM data stored in a []int16 slice, but to call libalsa I need that stored in a []byte slice.

How do I convert a []int16 slice to []byte to accomplish this?

You could try this:

package main

import "fmt"
import "bytes"
import "encoding/binary"

func main() {
    nums := [6]int16{2, 3, 5, 7, 11, 13}
    buf := new(bytes.Buffer)
    err := binary.Write(buf, binary.LittleEndian, nums)
    if(err==nil) {
        fmt.Printf("% x", buf.Bytes()) 
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM