Js字节数组问题

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);
        // 插入jsonArr
        array.set(jsonArr);
        // 插入radio Data
        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;
            }
        }
        // jsonStr
        let jsonStrArr = data.slice(0,splitIdx);
        let jsonStr=this.utf82str(jsonStrArr);
        // radioData
        let radioData = data.slice(splitIdx+1);
        console.log(radioData)
        // @ts-ignore
        return new DataModel(jsonStr,radioData);
    }

    public str2utf8(str) {
        // @ts-ignore
        let ec = new TextEncoder('utf8');
        // @ts-ignore
        return ec.encode(str);
    }

    public utf82str(data) {
        // @ts-ignore
        let dec = new TextDecoder('utf8');
        // @ts-ignore
        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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

詩不诉卿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值