使用struct-buffer为ArrayBuffer添加结构体

$ npm i struct-buffer

1. 创建结构体

import { DWORD, string_t, StructBuffer, uint32_t } from "struct-buffer";

const struct = new StructBuffer("Player",{
hp: DWORD, // 4字节大小
mp: uint32_t, // 4字节大小
name: string_t[3], // 3字节大小
});

2. 解析ArrayBuffer

const buffer = new Uint8Array([
0, 0, 0, 0x0a,
0, 0, 0, 0x64,
0x61, 0x62, 0x63,
]); const data = struct.decode(buffer);
// data => { hp: 10, mp: 100, name: 'abc' };

将Object数据转换为ArrayBuffer

// 注意key要和结构体定义时的一样
const view = struct.encode({
hp: 10,
mp: 100,
name: "abc",
});
// view => <00 00 00 0a 00 00 00 64 61 62 63>

可以注册类型

// registerType(typeName: string | string[], size: 1 | 2 | 4 | 8, unsigned = true): StructType

const short = registerType("short", 2, false);

可以使用typedef继承别的类型

// typedef(typeName: string | string[], type: StructType): StructType

const HANDLE = typedef("HANDLE", DWORD);

See alse:

js add Struct to ArrayBuffer的更多相关文章

  1. jquery add() 和js add()

    HTML DOM add() 方法 HTML DOM Select 对象 定义和用法 add() 方法用于向 <select> 添加一个 <option> 元素. 语法 sel ...

  2. [转]js add month 加n月

    本文转自:http://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript/5645126 I ...

  3. JS add script tag to dynamically call script

    //IE: var script = document.createElement("script"); script.setAttribute("type", ...

  4. [Node.js] Add Logging to a Node.js Application using Winston

    Winston is a popular logging library for NodeJS which allows you to customise the output, as well as ...

  5. leetcode—js—Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  6. js add media query

    var msViewportStyle = document.createElement("style"); msViewportStyle.appendChild( docume ...

  7. js,add script async? loaded ok.

    function loadScript(url, callback){ var script = document.createElement_x("script") script ...

  8. 简洁JS 日历控件 支持日期和月份选择

    原文出处 以下这个JS日历控件是我的闲暇之余自己编写的,所有的代码全部在IE7/IE8/Firefox下面测试通过, 而且可以解决被iframe层遮盖的问题.现在只提供两种风格(简洁版和古典版)和两种 ...

  9. [转] Creating a Simple RESTful Web App with Node.js, Express, and MongoDB

    You can find/fork the sample project on GitHub Hey! This and all my other tutorials will soon be mov ...

随机推荐

  1. FFT,NTT 笔记

    FFT 简介 FFT是干啥的?它是用来加速多项式乘法的.我们平时经常求多项式乘法,比如\((x+1)(x+3)=(x^2+4x+3)\).假设两个式子都是\(n\)项(不足的补0),那朴素的算法是\( ...

  2. ThinkPHP3.2.4 order方法注入

    漏洞详情: 漏洞文件:./ThinkPHP\Library\Think\Db\Driver.class.php 中的 parseOrder方法: 这也是继上次order方法注入之后的修复手段. 可以看 ...

  3. JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)

    关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...

  4. UML——基本结构

    一.宏观导图 学习UML的时候我们首先要把握好她的结构,基本上好料都在里面了.最重要的是构造块的学习. 公共机制:是为了让我们更加清楚的描述UML的各种关系.图.事物等. 规则:和语法的意思差不多,就 ...

  5. PHP-文件、目录相关操作

    PHP-文件.目录相关操作 一  目录操作(Directory 函数允许获得关于目录及其内容的信息) 相关函数: 函数 描述 chdir() 改变当前的目录. chroot() 改变根目录. clos ...

  6. 我们到底为什么要用 IoC 和 AOP

    作为一名 Java 开发,对 Spring 框架是再熟悉不过的了.Spring 支持的控制反转(Inversion of Control,缩写为IoC)和面向切面编程(Aspect-oriented ...

  7. 小白搭建WNMP详细教程---PHP安装与设置

    php的安装请参考WAMP中PHP的安装教程https://www.cnblogs.com/missbye/p/12049925.html 需要注意的是,我们下载的PHP版本要下载Non Thread ...

  8. httprunner(4)录制生成测试用例

    前言 写用例之前,我们应该熟悉API的详细信息.建议使用抓包工具Charles或AnyProxy进行抓包. har2case 我们先来了解一下另一个项目har2case 他的工作原理就是将当前主流的抓 ...

  9. POJ 2594 Treasure Exploration 最小可相交路径覆盖

    最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...

  10. Codeforces #Round 632 div2 A~C

                                       A. Little Artem   Young boy Artem tries to paint a picture, and h ...