简体   繁体   English

Go - int16 切片到字节切片

[英]Go - int16 slice to byte slice

I'm writing a Go program that interfaces with libalsa.我正在编写一个与 libalsa 接口的 Go 程序。 I have my PCM data stored in a []int16 slice, but to call libalsa I need that stored in a []byte slice.我将 PCM 数据存储在 []int16 切片中,但要调用 libalsa,我需要将其存储在 []byte 切片中。

How do I convert a []int16 slice to []byte to accomplish this?如何将 []int16 切片转换为 []byte来完成此操作?

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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