Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - Javascript类型的数组和字节序(Javascript Typed Arrays and Endianness)

I'm using WebGL to render a binary encoded mesh file.(我正在使用WebGL渲染二进制编码的网格文件。)

The binary file is written out in big-endian format (I can verify this by opening the file in a hex editor, or viewing the network traffic using fiddler).(二进制文件以big-endian格式写出(我可以通过在十六进制编辑器中打开文件或使用fiddler查看网络流量来验证这一点)。) When I try to read the binary response using a Float32Array or Int32Array, the binary is interpreted as little-endian and my values are wrong:(当我尝试使用Float32Array或Int32Array读取二进制响应时,二进制被解释为little-endian,而我的值是错误的:) // Interpret first 32bits in buffer as an int var wrongValue = new Int32Array(binaryArrayBuffer)[0]; I can't find any references to the default endianness of typed arrays in http://www.khronos.org/registry/typedarray/specs/latest/ so I'm wondering what's the deal?(我在http://www.khronos.org/registry/typedarray/specs/latest/中找不到对类型数组的默认字节序的任何引用,所以我想知道这是怎么回事?) Should I assume that all binary data should be little-endian when reading using typed arrays?(使用类型数组读取时,我是否应该假定所有二进制数据都应为低端字节序?) To get around the problem I can use a DataView object (discussed in the previous link) and call:(为了解决这个问题,我可以使用DataView对象(在上一个链接中讨论)并调用:) // Interpret first 32bits in buffer as an int var correctValue = new DataView(binaryArrayBuffer).getInt32(0); The DataView functions such as "getInt32" read big-endian values by default.(默认情况下,DataView函数(例如“ getInt32”)读取big-endian值。) (Note: I've tested using Google Chrome 15 and Firefox 8 and they both behave the same way)((注意:我已经使用Google Chrome 15和Firefox 8进行了测试,它们的行为方式相同))   ask by Bob translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The current behaviour, is determined by the endianness of the underlying hardware.(当前的行为取决于基础硬件的字节序。)

As almost all desktop computers are x86, this means little-endian.(由于几乎所有台式机都是x86,因此这意味着低位优先。) Most ARM OSes use little-endian mode (ARM processors are bi-endian and thus can operate in either).(大多数ARM操作系统使用低字节序模式(ARM处理器是双字节序的,因此可以在其中任何一种下运行)。) The reason why this is somewhat sad is the fact that it means almost nobody will test whether their code works on big-endian hardware, hurting what does, and the fact that the entire web platform was designed around code working uniformly across implementations and platforms, which this breaks.(之所以有些可悲,是因为这意味着几乎没有人会测试他们的代码是否可以在big-endian硬件上工作,从而损害了代码的工作能力,以及整个Web平台都是围绕代码在各个实现和平台之间统一工作而设计的,这打破了。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...