"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var FormatHelper = /** @class */ (function () {
    function FormatHelper() {
    }
    /**
     * Convert a string to byte buffer
     * @param str
     */
    FormatHelper.toByteBuffer = function (str) {
        var bufView = new Uint8Array(str.length);
        for (var i = 0, strLen = str.length; i < strLen; i++) {
            bufView[i] = str.charCodeAt(i);
        }
        return bufView;
    };
    FormatHelper.toHexString = function (buffer) {
        if (!buffer) {
            return "";
        }
        // Add check valid hex string
        var result = Array.prototype.map.call(buffer, function (x) { return ('00' + x.toString(16)).slice(-2); }).join('');
        return result.toUpperCase();
    };
    FormatHelper.toBinaryString = function (buffer) {
        var result = Array.prototype.map.call(buffer, function (x) {
            var res = "";
            for (var i = 7; i >= 0; i--) {
                res += ((0x1 << i) & x) > 0 ? "1" : "0";
            }
            return res;
        }).join('');
        return result;
    };
    FormatHelper.hexStringToBuffer = function (hexString) {
        var bytes = new Uint8Array(Math.ceil(hexString.length / 2));
        for (var i = 0; i < bytes.length; i++)
            bytes[i] = parseInt(hexString.substr(i * 2, 2), 16);
        return bytes;
    };
    /**
     * TODO add support for extended ascii
     */
    FormatHelper.toAsciiString = function (data) {
        var str = String.fromCharCode.apply(null, Array.from(data));
        return str;
    };
    FormatHelper.toUnsignedInt = function (data, sizeOf, lsb) {
        if (sizeOf === void 0) { sizeOf = 4; }
        if (lsb === void 0) { lsb = true; }
        var ret = 0;
        if (lsb) {
            data = data.reverse();
        }
        for (var i = 0; i < data.length && i < sizeOf; i++) {
            ret = (ret << 8) + (data[i] & 0xFF);
        }
        return ret;
    };
    return FormatHelper;
}());
exports.FormatHelper = FormatHelper;
