bytearrayconverter
v1.0.2
Published
`ByteConverter` 是一个 JavaScript 工具库,用于在 Node.js 和浏览器环境中方便地进行字节数组与多种数据类型之间的转换。支持小端和大端字节序,旨在简化字节数据处理的复杂性,提升开发效率。
Downloads
5
Readme
ByteConverter
ByteConverter 是一个用于在 JavaScript 中处理字节数组和基本数据类型之间转换的工具类。它支持在 Node.js 和浏览器环境中使用,并提供了一系列方法来处理不同类型的数据,包括字符串、整数、浮点数、布尔值和长整形等。通过扩展原型链,它使得数据类型的转换变得更加简便易用。
特性
- 字节序支持:支持小端(默认)和大端字节序的转换。
- 兼容性:兼容 Node.js 的
Buffer和浏览器的Uint8Array。 - 原型链扩展:通过扩展基本数据类型的原型链,允许直接在数字、布尔值和字符串等数据类型上调用转换方法,简化了操作。
- 多种数据类型转换:提供从字节数组到多种基本数据类型(如整数、浮点数、布尔值等)的转换。
- 字符串处理:支持将字符串转换为字节数组(UTF-8 编码)。
安装
如果你在 Node.js 环境中使用,可以通过以下命令安装:
npm install byteconverter用法示例
ByteConverter 的设计使得数据转换变得简单明了,以下是一些示例,突出其简便性:
从字节数组转换为基本数据类型
通过原型链扩展,用户可以直接在原生数组(Array)上调用转换方法,将字节数组转换为基本数据类型:
示例:将原生数组转换为浮点数
const floatBytes = [0, 0, 0, 64]; // 表示浮点数 2.0
const floatNumber = floatBytes.toFloat(); // 直接调用
console.log(floatNumber); // 输出: 2示例:将原生数组转换为整数
const intBytes = [57, 48, 0, 0]; // 表示整数 12345
const intNumber = intBytes.toInt(); // 直接调用
console.log(intNumber); // 输出: 12345示例:将原生数组转换为长整形
const longBytes = [1, 0, 0, 0, 0, 0, 0, 0]; // 表示长整型 1
const longNumber = longBytes.toLong(); // 直接调用
console.log(longNumber); // 输出: 1n示例:将原生数组转换为布尔值
const boolBytes = [1]; // 表示布尔值 true
const boolValue = boolBytes.toBool(); // 直接调用
console.log(boolValue); // 输出: true示例:将原生数组转换为字符串
const stringBytes = [72, 101, 108, 108, 111]; // "Hello" 的 UTF-8 编码
const str = stringBytes.toCustomString(); // 直接调用
console.log(str); // 输出: Hello基本数据类型转换为字节数组
使用扩展的 toBytes 方法,可以将基本数据类型直接转换为字节数组:
示例:将数字转换为字节数组
const number = 12345; // 要转换的数字
const bytes = number.toBytes(); // 直接调用
console.log(bytes); // 输出: Uint8Array(4) [57, 48, 0, 0]示例:将布尔值转换为字节数组
const bool = true; // 要转换的布尔值
const bytes = bool.toBytes(); // 直接调用
console.log(bytes); // 输出: Uint8Array(1) [1]示例:将字符串转换为字节数组
const str = "Hello"; // 要转换的字符串
const bytes = str.toBytes(); // 直接调用
console.log(bytes); // 输出: Uint8Array(5) [72, 101, 108, 108, 111]示例:将浮点数转换为字节数组
const floatNumber = 2.5; // 要转换的浮点数
const bytes = floatNumber.toBytes(); // 直接调用
console.log(bytes); // 输出: Uint8Array(4) [0, 0, 32, 64]示例:将长整型转换为字节数组
const longNumber = 1234567890123n; // 要转换的长整型
const bytes = longNumber.toBytes(); // 直接调用
console.log(bytes); // 输出: Uint8Array(8) [83, 184, 205, 50, 0, 0, 0, 0]方法列表
以下是 ByteConverter 类中提供的一些方法:
toString(bytes): 将字节数组转换为字符串。toFloat(bytes): 将字节数组转换为 32 位浮点数。toInt(bytes): 将字节数组转换为 32 位整数。toLong(bytes): 将字节数组转换为长整形(BigInt)。toBool(bytes): 将字节数组转换为布尔值。toDouble(bytes): 将字节数组转换为 64 位双精度浮点数。_boolToBytes(bool): 将布尔值转换为字节数组。_numberToBytes(number): 将数字转换为字节数组。_stringToBytes(string): 将字符串转换为字节数组。_toArrayBuffer(bytes): 将字节数组转换为ArrayBuffer。
原型链扩展的方法
以下方法被扩展到 JavaScript 原型链上,使得各数据类型可以直接使用:
Number.prototype.toBytes: 将数字转换为字节数组。Boolean.prototype.toBytes: 将布尔值转换为字节数组。String.prototype.toBytes: 将字符串转换为字节数组。Uint8Array.prototype.toCustomString: 将字节数组转换为字符串。Uint8Array.prototype.toFloat: 将字节数组转换为浮点数。Uint8Array.prototype.toInt: 将字节数组转换为整数。Uint8Array.prototype.toLong: 将字节数组转换为长整形。Uint8Array.prototype.toBool: 将字节数组转换为布尔值。Uint8Array.prototype.toDouble: 将字节数组转换为双精度浮点数。
TODO
对bytes数组混淆支持 ABCD CDAB BADC DCBA
贡献
欢迎提交问题、建议或拉取请求以改进此库。
许可证
此项目采用 MIT 许可证,详情请查看 LICENSE。
