使用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. yum安装docker-ce-18.03.0

    yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo http://mir ...

  2. Cmder的findstr问题

    在环境变量中加入C:\windows\system32即可

  3. ShowDoc,APIDoc,可道云API,语雀-适合IT企业的文档工具

    ShowDoc,APIDoc,可道云API,语雀-适合IT企业的文档工具 一.ShowDoc官方文档及说明 1.1 它可以用来做什么 1.2 它都有些什么功能 1.3 使用在线的ShowDoc 1.4 ...

  4. Java——数据类型

    数据类型分类 基本数据类型: 数值型: 整数类型(byte,short,int,long): 浮点类型(float,double): 字符型(char): 布尔值(boolean): 引用数据类型: ...

  5. 关于POI相关通用方法源码

    设置宽度,1个汉字的宽度 导入excel用,返回行数 sheetName是sheet,显示名 导出excel 导出excel 获得excel数据 写输出,最后用 重新单元格指定位置 移到下一行,列开头 ...

  6. HDU-4773 Problem of Apollonius (圆的反演)

    参考: https://oi-wiki.org/geometry/inverse/ https://blog.csdn.net/acdreamers/article/details/16966369 ...

  7. Preliminaries for Benelux Algorithm Programming Contest 2019

    A. Architecture 如果行最大值中的最大值和列最大值中的最大值不同的话,那么一定会产生矛盾,可以手模一个样例看看. 当满足行列最大值相同条件的时候,就可以判定了. 因为其余的地方一定可以构 ...

  8. AcWing 241 楼兰图腾 (树状数组)

    在完成了分配任务之后,西部314来到了楼兰古城的西部. 相传很久以前这片土地上(比楼兰古城还早)生活着两个部落,一个部落崇拜尖刀('V'),一个部落崇拜铁锹('∧'),他们分别用V和∧的形状来代表各自 ...

  9. Gym - 102062A、B、C、D、E、F、G、H

    比赛链接:https://vjudge.net/contest/409725#problem 题面点此处进入 Gym - 102062A 题意: 就是说比赛一共发a+b+c+d个牌子,现在不带上主人公 ...

  10. L2-007 家庭房产 (25分) 并查集

    题目链接 题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去.这个题有个坑编号可能为0000,会错数据3和5. 1 #include<bits/stdc++.h> 2 usin ...