ToInt16Array converts the value to int16 slice if it's an Int16Array.
()
| 889 | |
| 890 | // ToInt16Array converts the value to int16 slice if it's an Int16Array. |
| 891 | func (v *Value) ToInt16Array() ([]int16, error) { |
| 892 | if !v.IsInt16Array() { |
| 893 | return nil, errors.New("value is not an Int16Array") |
| 894 | } |
| 895 | |
| 896 | buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo() |
| 897 | defer buffer.Free() |
| 898 | |
| 899 | totalSize := uint(byteOffset + byteLength) |
| 900 | bytes, err := buffer.ToByteArray(totalSize) |
| 901 | if err != nil { |
| 902 | return nil, err |
| 903 | } |
| 904 | |
| 905 | data := bytes[byteOffset : byteOffset+byteLength] |
| 906 | result := make([]int16, len(data)/2) |
| 907 | for i := 0; i < len(result); i++ { |
| 908 | result[i] = int16(binary.LittleEndian.Uint16(data[i*2:])) |
| 909 | } |
| 910 | return result, nil |
| 911 | } |
| 912 | |
| 913 | // ToUint16Array converts the value to uint16 slice if it's a Uint16Array. |
| 914 | func (v *Value) ToUint16Array() ([]uint16, error) { |