Js字节数组问题
前后端进行字节数组对接的问题
后端
后端采用C#编写(具体使用语言看要求)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFmpegDemo.Protocol
{
/// <summary>
/// 后端协议
/// </summary>
public class ProtocolDefalut
{
//Json数据 + 0x00 + 扩展数据(byte数组)
//{“a”:1} + byte[]
//0x7B,0x22,0x61,0x22,0x3A,0x31,0x7D,0x00,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn,0xnn
/// <summary>
/// 协议对象
/// </summary>
public class ProtocolDataModel
{
/// <summary>
/// json数据
/// </summary>
public string JsonStr { get; set; }
/// <summary>
/// 扩展数据
/// </summary>
public byte[] ExtendData { get; set; } = new byte[0];
}
public Encoding TextEncoding { get; set; } = Encoding.UTF8;
/// <summary>
/// 构建 buffer
/// </summary>
/// <returns></returns>
public byte[] Build(ProtocolDataModel model)
{
var jsonBytes = TextEncoding.GetBytes(model.JsonStr);
var buffer = new byte[jsonBytes.Length + 1 + model.ExtendData.Length];
//json部分
var jsonStartIndex = 0;
var jsonLength = jsonBytes.Length;
Array.Copy(jsonBytes, 0, buffer, jsonStartIndex, jsonLength);
//分割符
var splitIndex = jsonBytes.Length;
buffer[splitIndex] = 0x00;
//扩展部分
var extendStartIndex = splitIndex + 1;
var extendLength = model.ExtendData.Length;
Array.Copy(model.ExtendData, 0, buffer, extendStartIndex, extendLength);
return buffer;
}
/// <summary>
/// 解构 buffer
/// </summary>
/// <param name="datas"></param>
/// <returns></returns>
public ProtocolDataModel UnBuild(byte[] datas)
{
//找0x00进行分割
var jsonLenth = datas.Length;
var extendLength = 0;
for (int i = 0; i < datas.Length; i++)
{
if(datas[i] == 0x00)
{
jsonLenth = i;
break;
}
}
extendLength = datas.Length - jsonLenth - 1;
if(extendLength < 0)
{
extendLength = 0;
}
var jsonBytes = new byte[jsonLenth];
var extendBytes = new byte[extendLength];
//json部分
var jsonStartIndex = 0;
var jsonLength = jsonBytes.Length;
Array.Copy(datas, jsonStartIndex, jsonBytes, 0, jsonLength);
//分割符
var splitIndex = jsonBytes.Length;
//xxxxxx
//扩展部分
if(extendLength > 0)
{
var extendStartIndex = splitIndex + 1;
Array.Copy(datas, extendStartIndex, extendBytes, 0, extendLength);
}
var model = new ProtocolDataModel()
{
JsonStr = TextEncoding.GetString(jsonBytes),
ExtendData = extendBytes,
};
return model;
}
}
}
前端
前端采用js进行解析(用的是ts编写,然后转js)
class DataModel {
public jsonStr: string;
public radioData: number[];
constructor(jsonStr: string, radioData: number[]) {
this.jsonStr = jsonStr;
this.radioData = radioData;
}
}
class RadioProtocol {
public buildPack = (model: DataModel) => {
let jsonArr = this.str2utf8(model.jsonStr);
let array = new Uint8Array(model.radioData.length + 1 + jsonArr.length);
array.set(jsonArr);
array.set(model.radioData, jsonArr.length + 1);
return array;
}
public unBuildPack = (data: Uint8Array) => {
console.log(data)
let splitIdx = data.length;
for (let i = 0; i < data.length; i++) {
if (data[i] === 0x00) {
splitIdx=i;
}
}
let jsonStrArr = data.slice(0,splitIdx);
let jsonStr=this.utf82str(jsonStrArr);
let radioData = data.slice(splitIdx+1);
console.log(radioData)
return new DataModel(jsonStr,radioData);
}
public str2utf8(str) {
let ec = new TextEncoder('utf8');
return ec.encode(str);
}
public utf82str(data) {
let dec = new TextDecoder('utf8');
return dec.decode(data);
}
}
let model = new DataModel('{“a”:1}', [0x7B, 0x22, 0x61, 0x22, 0x3A, 0x31, 0x7D, 0x01]);
let protocol = new RadioProtocol();
let data = protocol.buildPack(model);
let pack = protocol.unBuildPack(data);
console.log(pack)
|