JS版本的ByteArray实现

2020-01-02 15:22:52

<p>首先要感谢Egret,继续传承AS3</p><p><br></p><p><a href="http://labs.shareme.cn/labs/bytearray" rel="noopener noreferrer" target="_blank">ByteArray的bigEndian与littleEndian用法</a>,(HTML5的 DataView 也抄了,也支持这玩意)</p><p><br></p><p><a href="http://www.shareme.cn/assets/labs/ByteArray.js" rel="noopener noreferrer" target="_blank">ByteArray.js</a> 下载</p><pre class="ql-syntax" spellcheck="false"><span class="hljs-keyword">var</span> len = <span class="hljs-number">0x01020304</span>; <span class="hljs-keyword">var</span> id = <span class="hljs-number">0xFF000000</span>; <span class="hljs-keyword">var</span> uid = <span class="hljs-number">0xFF</span>; <span class="hljs-keyword">var</span> head = <span class="hljs-keyword">new</span> ByteArray(); head.writeUnsignedInt(len); head.writeUnsignedInt(id); head.writeUnsignedInt(uid); <span class="hljs-comment">//console.log(head.bytes)</span> <span class="hljs-comment">//结果:</span> <span class="hljs-comment">//1,2,3,4,255,0,0,0,0,0,0,255</span> <span class="hljs-keyword">var</span> head = <span class="hljs-keyword">new</span> ByteArray(); head.endian = Endian.LITTLE_ENDIAN; head.writeUnsignedInt(len); head.writeUnsignedInt(id); head.writeUnsignedInt(uid); <span class="hljs-comment">//console.log(head.bytes)</span> <span class="hljs-comment">//结果:</span> <span class="hljs-comment">//4,3,2,1,0,0,0,255,255,0,0,0</span> </pre><p><br></p><p><br></p>