# 十六进制/ArrayBuffer/ASCII/modbus/crc/高低位,常用于串口、蓝牙、socket、UDP等字节数据转换

## 业务定制需求可加Q252797991

## 您的好评就是插件更新的动力，麻烦亲动动手指给个好评!!!!

## byte字节工具类

```

import * as ByteUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-ByteUtils.js';

```

- 整型转字节

```

				let intValue = 500;
				let byteValue = ByteUtils.intToByte(intValue);

```

- 字节转整型

```

				let byteValue = 100;
				let intValue = ByteUtils.byteToInt(byteValue);

```

## hex十六进制工具类

```

import * as HexUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-HexUtils.js';

```

- 十进制数组转十六进制字符串

```

				let intArray = [];
				intArray.push(0x01);
				intArray.push(355);
				let hex = HexUtils.intArrayToHex(intArray);

```

- 字节数组转十六进制字符串

```

				let byteArray = [];
				byteArray.push(0x01);
				byteArray.push(35);
				let hex = HexUtils.byteArrayToHex(byteArray);

```

- 十六进制字符串转整型数组

```

	let hex = "030610aabb"
				let intArray = HexUtils.hexToIntArray(hex);

```

- 十六进制字符串转字节数组

```

				let hex = "ffbb02"
				let byteArray = HexUtils.hexToByteArray(hex);

```

- 十六进制字符串转arrayBuffer

```

				let hex = "ffbb02"
				let arrayBuffer = HexUtils.hexToArrayBuffer(hex);

```

- arrayBuffer转十六进制字符串

```

				let byteArrayBuffer = new Uint8Array([0x05, 0x02, 0xff]);
				let arrayBuffer = byteArrayBuffer.buffer;
				let hex = HexUtils.arrayBufferToHex(arrayBuffer);

```

- 十六进制字符串转Uint8Array

```

				let hex = "030610aabb"
				let uint8Array = HexUtils.hexToUint8Array(hex);
				let msg = "hexToUint8Array\n" + "hex:" + hex + "\nintArray:" + uint8Array;
				this.showMsg(msg);

```

- Uint8Array转十六进制字符串

```

				var uint8 = new Uint8Array(4);
				uint8[0] = 0x1f;
				uint8[1] = 0x2f;
				uint8[2] = 0x3f;
				uint8[3] = 0x4f;
				let hex = HexUtils.uint8ArrayToHex(uint8);
				let msg = "uint8ArrayToHex:" + uint8 + " uint8ArrayToHex hex:" + hex;
				this.showMsg(msg);

```

## CRC校验工具类

```

import * as CRCUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-CRCUtils.js';

```

- 获取CRC16_MODBUS

```

				let hex = "05060A";
				let data = HexUtils.hexToByteArray(hex);
				var crc = CRCUtils.getCRC16_MODBUS(data);
				// CRC十六进制
				var crcHex = IntUtils.intToHex(crc)
				var obj = CRCUtils.intToHighLow(crc);
				// CRC低位
				let low = obj.low;
				// CRC高位
				let high = obj.high

```

- 获取CRC16_CCITT_FALSE

```

			let hex = "05060A";	
			let data = HexUtils.hexToByteArray(hex);
			var crc = CRCUtils.getCRC16_CCITT_FALSE(data);
			// CRC十六进制
			var crcHex = IntUtils.intToHex(crc)
			var obj = CRCUtils.intToHighLow(crc);
			// CRC低位
			let low = obj.low;
			// CRC高位
			let high = obj.high

```

- 获取CRC32

```

				let data = [0x05, 0x06, 0x0a];
				let initialCrc = 0xFFFFFFFF;
				let polynom = 0x04C11DB7;
				let xor = 0xFFFFFFFF;
				let refIn = true;
				let refOut = true;
				var crc = CRCUtils.getCRC32(data);
				this.showMsg("CRC32:" + crc)


```

## modubs工具类

```

import * as ModbusUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-ModbusUtils.js';

```
- 写指令

```

				// 设备地址
				let address = 0x25;
				// 寄存器起始地址
				let start = 20;
				// 寄存器个数
				let count = 3;
				let byteArray = ModbusUtils.getReadModbus(address, start, count);
				let hex = HexUtils.byteArrayToHex(byteArray);
				
				// 如果CRC高位在前
				let crcLowFront = false; // CRC是否低位在前
				let byteArray1 = ModbusUtils.getReadModbusCRCLowFront(address, start, count, crcLowFront);
				let hex1 = HexUtils.byteArrayToHex(byteArray1);

```

- 读指令

```

				// 设备地址
				let address = 0x25;
				// 寄存器起始地址
				let start = 20;
				// 数据
				let data = [];
				data.push(0x02);
				data.push(0x0A);
				let byteArray = ModbusUtils.getWriteModbus(address, start, data);
				let hex = HexUtils.byteArrayToHex(byteArray);

				// 如果CRC高位在前
				let crcLowFront = false; // CRC是否低位在前
				let byteArray1 = ModbusUtils.getWriteModbusCRCLowFront(address, start, data, crcLowFront);
				let hex1 = HexUtils.byteArrayToHex(byteArray1);

```

## ASCII工具类

```

import * as ASCIIUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-ASCIIUtils.js';

```

- 字符串转ASCII

```

				let str = "Get_ID占三";
				// byteArray为字节数组
				let byteArray = ASCIIUtils.encodeUtf8(str);
				// hex为十六进制字符串
				let hex = HexUtils.byteArrayToHex(byteArray);

```

- ASCII转字符串

```

let hex = "4765745f4944e58da0e4b889"
let decodeStr = ASCIIUtils.decodeUtf8(hex);

```

## 整型工具类

```

import * as IntUtils from '@/uni_modules/wrs-js-modbusCRCHex/js_sdk/wrs-IntUtils.js';

```

- 高低位转整型

```

				// 低位
				let low = 226;
				// 高位
				let high = 102;
				// 26338
				let value = IntUtils.highLowToInt({
					low: low,
					high: high
				})
				
```

- 整型转高低位

```

				// 整型
				let value = 26338;
				// 26338
				let obj = IntUtils.intToHighLow(value)
				// 低位
				let low = obj.low;
				// 高位
				let high = obj.high;

```

- 大端序-字节数组转整型

```

var byteArray = [0x01, 0x02, 0x03, 0x04]
var intValue = IntUtils.bigEndianToInt(byteArray)

```

- 大端序 int转字节数组

```

	var intValue = 2461;
	var byteArray = IntUtils.bigEndianToByteArray(intValue)

```

- 小端序-字节数组转整型

```

			var byteArray = [0x01, 0x02, 0x03, 0x04]
			var intValue = IntUtils.littleEndianToInt(byteArray)

```

- 小端序-整型转字节数组

```

var intValue = 2461;
var byteArray = IntUtils.littleEndianToByteArray(intValue)
				

```

- 十进制转二进制

```

			var intValue = 20;
			var bitStr = IntUtils.intToBit(intValue);

```

- 二进制转十进制

```

				var bit = "00001001";
				var intValue = IntUtils.bitToInt(bit);


```