简体   繁体   English

将Array转换为ArrayBuffer时如何获取dataType?

[英]How can i get the dataType when Array is castet into an ArrayBuffer?

I have an Array: 我有一个数组:

var dataArray = new Uint16Array(256);

then my array goes over nodeJs to an other pc. 然后我的阵列通过nodeJs转到另一台PC。 (nodejs binaryType = "arraybuffer") (nodejs binaryType =“ arraybuffer”)

How do I know on the other client that i have to cast to Uint16Array and not Uint8Array? 我如何在另一个客户端上知道必须转换为Uint16Array而不是Uint8Array?

Note: the bit depth and array length is a dynamic value and can change while programm is running. 注意:位深度和数组长度是一个动态值,可以在程序运行时更改。

Thanks :) 谢谢 :)

When transferring data as ArrayBuffer its just binary buffer data, you need to know with what view you want to access the data with. 当将数据作为ArrayBuffer的二进制缓冲区数据传输时,您需要知道要使用哪个视图访问数据。 TypedArrays are just a view on a binary ArrayBuffer . TypedArrays只是二进制ArrayBuffer上的视图。

There are several ways to handle this: 有几种方法可以解决此问题:

  • Use different routes to submit your data 使用不同的路线提交您的数据
  • Add a prefix byte to your data 在数据中添加一个前缀字节
  • And possibly the best solution, do not change your data type mid application. 可能是最好的解决方案,不要在应用程序中间更改数据类型。 imho a clean interface should only handle one type of data. imho一个干净的接口应该只处理一种类型的数据。

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

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