Convert hex-string to byte array
@param {Array|Uint8Array|ArrayBuffer} arr
@return {string}
327402
function bytesToHex(arr) {
    if (arr instanceof ArrayBuffer) {
        arr = new Uint8Array(arr);
    }
    var str = '';
    for (var i = 0; i < arr.length; i++) {
        var byte = arr[i].toString(16);
        if (byte.length === 1) {
            str += '0';
        }
        str += byte;
    }
    return str;
}
1183219
function (buffer) {
        var str = '',
            uArrayVal = new Uint8Array(buffer),

            i,
            ii;

        for (i = 0, ii = uArrayVal.length; i < ii; i++) {
            str += String.fromCharCode(uArrayVal[i]);
        }

        return str;
    }
579168
function encode(hexString) {
	// Convert to array of bytes
	var bytes = Buffer.from(hexString, "hex");
	var encoded = base32.stringify(bytes);

	// strip padding & lowercase
	return encoded.replace(/(=+)$/, '').toLowerCase();
}
1190636
function TrimArrayToBytes(arr) {
    var ix, newarr;
    var len = arr.length;
    for (ix=0; ix<len; ix++) {
        if (arr[ix] < 0 || arr[ix] >= 0x100) 
            break;
    }
    if (ix == len) {
        return arr;
    }
    newarr = Array(len);
    for (ix=0; ix<len; ix++) {
        if (arr[ix] < 0 || arr[ix] >= 0x100) 
            newarr[ix] = 63;  // '?'
        else
            newarr[ix] = arr[ix];
    }
    return newarr;
}
997901
private static byte[] hexStringToByteArray(String s)
	{
		byte[] b = new byte[s.length() / 2];
		for (int i = 0; i < b.length; i++)
		{
			int index = i * 2;
			int v = Integer.parseInt(s.substring(index, index + 2), 16);
			b[i] = (byte) v;
		}
		return b;
	}
145804
function (str) {
                str = stringify(str);
                var byteArray = [];

                for (var i = 0, l = str.length, ch; i < l; i++) {
                    ch = str.charCodeAt(i);
                    byteArray.push(ch >> 8);
                    byteArray.push(ch & 0xFF);
                }

                return byteArray;
            }
441961
function (arr) {
    var out = "", i;
    for (i=0; i<arr.length; i++) {
      out += ((arr[i]|0)+0xF00000000000).toString(16).substr(4);
    }
    return out.substr(0, sjcl.bitArray.bitLength(arr)/4);//.replace(/(.{8})/g, "$1 ");
  }
177499
function stringToArrayBuffer(string) {
            var
                buffer = new ArrayBuffer(string.length),
                int8Array = new Uint8Array(buffer);
            
            for (var i = 0; i < string.length; i++) {
                int8Array[i] = string.charCodeAt(i);
            }
            
            return buffer;
        }
1335292
function stringToArrayBuffer(text) {
      if (window.TextEncoder !== undefined) {
        return new TextEncoder().encode(text).buffer;
      }

      var array = new Uint8Array(new ArrayBuffer(text.length));

      for (var i = 0, il = text.length; i < il; i++) {
        var value = text.charCodeAt(i);

        // Replacing multi-byte character with space(0x20).
        array[i] = value > 0xff ? 0x20 : value;
      }

      return array.buffer;
    }
262420
static byte[] fromHexString(String str) {
    if (str.length() % 2 != 0) {
      throw new InvalidOperationException("Hex-string has odd length!");
    }
    byte[] data = new byte[str.length() / 2];
    int spos = 0;
    for (int dpos = 0; dpos < data.length; dpos++) {
      int d1 = Character.digit(str.charAt(spos++), 16);
      int d2 = Character.digit(str.charAt(spos++), 16);
      if (d1 < 0 || d2 < 0) {
        throw new InvalidOperationException("Mal-formed hex-string!");
      }
      data[dpos] = (byte) ((d1 << 4) | d2);
    }
    return data;
  }
694099
function stringToBytes(str) {
  var bytes = new Array();
  var len, c;
  len = str.length;
  for (var i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if (c >= 0x010000 && c <= 0x10FFFF) {
      bytes.push(((c >> 18) & 0x07) | 0xF0);
      bytes.push(((c >> 12) & 0x3F) | 0x80);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000800 && c <= 0x00FFFF) {
      bytes.push(((c >> 12) & 0x0F) | 0xE0);
      bytes.push(((c >> 6) & 0x3F) | 0x80);
      bytes.push((c & 0x3F) | 0x80);
    } else if (c >= 0x000080 && c <= 0x0007FF) {
      bytes.push(((c >> 6) & 0x1F) | 0xC0);
      bytes.push((c & 0x3F) | 0x80);
    } else {
      bytes.push(c & 0xFF);
    }
  }
  return bytes;

}
924249
function toHex(i) {
    var hex;

    if (i < 0) {
        i += 256;
    }
    hex = i.toString(16);

    // zero padding
    if (hex.length == 1) {
        hex = "0" + hex;
    }
    return hex;
}
222501
function bytesToBase64(arr) {
    if (arr instanceof ArrayBuffer) {
        arr = new Uint8Array(arr);
    }
    if (typeof btoa === 'undefined' && typeof Buffer === 'function') {
        // node.js doesn't have btoa
        var buffer = Buffer.from(arr);
        return buffer.toString('base64');
    }
    var str = '';
    for (var i = 0; i < arr.length; i++) {
        str += String.fromCharCode(arr[i]);
    }
    return btoa(str);
}
395652
function uint8ArrayToArrayBuffer (arr) {
  return arr.buffer.slice(arr.byteOffset, arr.byteLength + arr.byteOffset)
}
392731
function arrayToBuffer(arr) {
    if (arr instanceof ArrayBuffer) {
        return arr;
    }
    var ab = arr.buffer;
    if (arr.byteOffset === 0 && arr.byteLength === ab.byteLength) {
        return ab;
    }
    return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength);
}
1358179
function toDebug(arr) {
                var binary = toBinaryString(arr);
                var str = '';
                for (var i = 0; i < arr.length; i++) {
                    str += '0000'.substr((i * 32).toString(16).length) + (i * 32).toString(16) + ':';
                    for (var j = 0; j < 32; j += 4) {
                        str += ' ' + binary.substr(i * 32 + j, 4);
                    }
                    if (i < arr.length - 1) str += '\n';
                }
                return str
            }
649335
function byteArrayToString(byteArray) {
		let str = '';
		for (let i = 0; i < byteArray.length; i++) {
			str += byteArray[i] <= 0x7F ? byteArray[i] === 0x25 ? '%25' : // %
				String.fromCharCode(byteArray[i]) : '%' + byteArray[i].toString(16).toUpperCase();
		}

		return decodeURIComponent(str);
	}