aardio寫的16進制解析庫

使用aardio寫了一個udp測試工具,使用的hpsocket,記錄一下

他好像是直接封裝的c還是c++的庫,所以返回的數據其實是一個指針數據,需要轉爲Byte[]數組,然後再做相關的解析操作

下面是寫測試工具過程中用到的寫的進制轉換類

namespace realid.iohelper {
    //創建byte[]數組
    createBuffer = function(bufLength) {
        return ..raw.buffer(bufLength);
    };
    //複製數組
    copyBuffer = function(sourceBuffers, startIndex, copyLength) {
        var destinationBuffer = createBuffer(copyLength);
        var index = 1;
        var endIndex = startIndex + copyLength - 1;
        for (i = startIndex; endIndex; 1) {
            
            destinationBuffer[index] = sourceBuffers[i];
            index++;
        }

        return destinationBuffer;
    };
    //指針數pointData據轉byte[]
    point2Hex = function(pointData, startPosition, endPosition) {
        var data = ..raw.tostring(pointData, startPosition, endPosition);
        var dataHex = ..string.hex(data, "");
        return dataHex;
    };
    //指針數pointData據轉hex字符串
    point2Buffer = function(pointData, length) {
        var buffers = ..raw.buffer(length);
            ..raw.copy(buffers, pointData, length);
        return buffers;
    };
    //翻轉數組
    reverse = function(buffers) {
        var array = createBuffer(#buffers);
        var index = 1;
        for (i = #buffers; 1; - 1) {
            array[index] = buffers[i];
            index++;
        }
        return array;
    };
    //單byte轉bitarray
    byte2BitArray = function(byteData) {
        var bitSet = {};
        for (i = 0; 7; 1) {
                ..table.push(bitSet, ((byteData >> i) & 0x1));
        }

        return bitSet;
    };
    //byte[]轉bitarray
    buffer2BitArray = function(buffers, isLittleEndian = true) {
        var bitSet = {};
        if (isLittleEndian) {
            for (j = #buffers; 1; - 1) {
                var byteData = buffers[j];
                for (i = 0; 7; 1) {
                     ..table.push(bitSet, ((byteData >> i) & 0x1));
                }
            }

        } else {
           for (j =1; #buffers; 1) {
                var byteData = buffers[j];
                for (i =7; 1;-1) {
                        ..table.push(bitSet, ((byteData >> i) & 0x1));
                }
            }
        }
        return bitSet;
    };
    //buffer轉hex
    buffer2Hex = function(buffers) {
        var hex = "";
        import console;
        for (i = 1;# buffers; 1) {
            var byteHex1 = ..tostring(buffers[i], 16); //類型 0xa1 ox0;
            var byteHex2 = "00"; //轉爲 a100
            if (byteHex1 != "0x0") {
                byteHex2 = ..string.trimleft(byteHex1, "0x"); //轉爲 a100
                if (#byteHex2 == 1) {
                    byteHex2 = ..string.concat("0", byteHex2);
                }
            };
            hex = ..string.concat(hex, byteHex2);
        }
        return ..string.upper(hex);
    };
    //hex轉buffers
    hex2Buffer = function(hex) {
        hex = ..string.replace(hex, "@-", "");
        hex = ..string.replace(hex, "@0x", "");
        hex = ..string.replace(hex, "@0X", "");
        hex = ..string.replace(hex, "@ ", "");
        hex = ..string.replace(hex, "@\r", "");
        hex = ..string.replace(hex, "@\n", "");
        hex = ..string.replace(hex, "@,", "");
        
        var dataLength = #hex;
        var bts = createBuffer(dataLength / 2);
        var index = 1;
        for (i = 1; dataLength; 2) {
            var strHex = ..string.slice(hex, i, i + 1);
            bts[index] = ..tonumber(strHex, 16);
            index++;
        }
        return bts;

    }
    //從字節數據指定位置讀取一個無符號16位整數
    getInt = function(buffers, isLittleEndian = true) {
        var hex = "";
        if (isLittleEndian) {
            var newBuffers = reverse(buffers);
            hex = buffer2Hex(newBuffers);

        } else {
            hex = buffer2Hex(buffers);
        }
        return ..tonumber(hex, 16);
    };
    getNow = function() {
        var tm = ..time();
        //改變格式化模式串
        tm.format = "%Y-%m-%d %H:%M:%S";

        //從時間值創建字符串
        var str = ..tostring(tm);
        return str;
    };
    getNowZH = function() {
        var tm = ..time();
        //改變格式化模式串
        tm.format = "%Y年%m月%d日 %H點%M分%S秒";

        //從時間值創建字符串
        var str = ..tostring(tm);
        return str;
    }

}

/**intellisense(realid.iothelper)
createBuffer(bufLength) = 創建byte[]數組
point2Buffer(pointData,length) = 指針數pointData據轉byte[]
copyBuffer(sourceBuffers,startIndex,endIndex) = 複製數組
reverse(buffers) =翻轉數組
byte2BitArray(byte) =單個byte轉bitArray
buffer2BitArray(buffers, isLittleEndian = true)=byte[]數組轉bitarray
point2Hex(pointData,startPosition,copyLength) = 指針數pointData據轉hex字符串
buffer2Hex(buffers) =將byte[]數組轉爲HEX字符串
hex2Buffer(hexString) =將HEX字符串轉爲byte[]數組
getInt(buffers,isLittleEndian=true) =從字節數據指定位置讀取一個無符號16位整數
getNow() = 獲取當前時間2021-04-10 13:32:20
getNowZH() = 獲取中文格式2021年4月10號 13點32分20秒
end intellisense**/

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章