gopcmlibalsa

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?


Solution

  • 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()) 
        }
    
    }