/* Convert an array of 32-bit Unicode values to an array of 8-bit byte
values, encoded as big-endian words.
349085
function UniArrayToBE32(arr) {
    var res = new Array(4*arr.length);
    for (var ix=0; ix<arr.length; ix++) {
        var val = arr[ix];
        res[4*ix]   = (val >> 24) & 0xFF;
        res[4*ix+1] = (val >> 16) & 0xFF;
        res[4*ix+2] = (val >> 8) & 0xFF;
        res[4*ix+3] = (val) & 0xFF;
    }
    return res;
}
82676
private static byte[] toByteArrayReverse(BitSet bits) {
        byte[] bytes = new byte[bits.length() / 8 + 1];
        for (int i = 0; i < bits.length(); i++) {
            if (bits.get(i)) {
                final int value = bytes[i / 8] | (1 << (7 - (i % 8)));
                bytes[i / 8] = (byte) value;
            }
        }
        return bytes;
    }
888086
def _convert_1bit_array_to_byte_array(arr):
    
    # Padding if necessary
    while len(arr) < 8 or len(arr) % 8:
        arr.append(0)

    arr = _np.array(arr, dtype='uint8')
    bit_arr = []
    idx = 0
    # Iterate and combine 8-bits into a uint8
    for arr_idx in range(int(len(arr) / 8)):
        bit_arr.append(((arr[idx] << 7) & (1 << 7)) |
                        ((arr[idx+1] << 6) & (1 << 6)) |
                        ((arr[idx+2] << 5) & (1 << 5)) |
                        ((arr[idx+3] << 4) & (1 << 4)) |
                        ((arr[idx+4] << 3) & (1 << 3)) |
                        ((arr[idx+5] << 2) & (1 << 2)) |
                        ((arr[idx+6] << 1) & (1 << 1)) |
                        ((arr[idx+7] << 0) & (1 << 0))
                        )
        idx += 8
    return _np.array(bit_arr, dtype='uint8')
444010
function b64toBlob(b64Data, contentType, sliceSize) {
	contentType = contentType || '';
	sliceSize = sliceSize || 512;

	var byteCharacters = atob(b64Data);
	var byteArrays = [];

	for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
		var slice = byteCharacters.slice(offset, offset + sliceSize);

		var byteNumbers = new Array(slice.length);
		for (var i = 0; i < slice.length; i++) {
			byteNumbers[i] = slice.charCodeAt(i);
		}

		var byteArray = new Uint8Array(byteNumbers);

		byteArrays.push(byteArray);
	}

	var blob = new Blob(byteArrays, {type: contentType});
	return blob;
}
938129
public static byte[] encodeAsBytes(byte[] inArray) {

		// Output string must be 4 * floor((n+2)/3) large
		byte[] out = new byte[4 * ((inArray.length+2)/3)];
		// Create padded input array with the next largest length that is a multiple of 3
		byte[] in = new byte[(inArray.length+2)/3*3];
		// Copy content form unpadded to padded array
		System.arraycopy(inArray, 0, in, 0, inArray.length);

		int outi = 0;
		for(int i = 0; i < in.length; i+=3) {

			out[outi++] = (byte)((in[i] & 0xFF) >>> 2);
			out[outi++] = (byte)(((in[i] & 0x03) << 4) | ((in[i+1] & 0xFF) >>> 4));
			out[outi++] = (byte)(((in[i+1] & 0x0F) << 2) | ((in[i+2] & 0xFF) >>> 6));
			out[outi++] = (byte)(in[i+2] & 0x3F);

		}

		for(int i = in.length - inArray.length; i > 0; i--) {
			out[out.length - i] = -1;
		}

		return out;
	}
547083
public static byte[] longToBytes(long v, final byte[] arr) {
    for (int i = 0; i < 8; i++) {
      arr[i] = (byte) (v & 0XFFL);
      v >>>= 8;
    }
    return arr;
  }
1365587
function bnToByteArray(encode_sign_bit) {
  var i = this.t, r = new Array();
  r[0] = this.s;
  var p = this.DB-(i*this.DB)%8, d, k = 0;
  if(i-- > 0) {
    if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p)
      r[k++] = d|(this.s<<(this.DB-p));
    while(i >= 0) {
      if(p < 8) {
        d = (this[i]&((1<<p)-1))<<(8-p);
        d |= this[--i]>>(p+=this.DB-8);
      }
      else {
        d = (this[i]>>(p-=8))&0xff;
        if(p <= 0) { p += this.DB; --i; }
      }
      if (encode_sign_bit) {
        if((d&0x80) != 0) d |= -256;
        if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
      }
      if(k > 0 || d != this.s) r[k++] = d;
    }
  }
  return r;
}
563487
function UniArrayToUTF8(arr) {
    var count = 0;

    for (var ix=0; ix<arr.length; ix++) {
        var val = arr[ix];
        if (val < 0x80) {
            count += 1;
        }
        else if (val < 0x800) {
            count += 2;
        }
        else if (val < 0x10000) {
            count += 3;
        }
        else if (val < 0x200000) {
            count += 4;
        }
        else {
            count += 1;
        }
    }

    if (count == arr.length)
        return arr;

    var res = [];
    for (var ix=0; ix<arr.length; ix++) {
        var val = arr[ix];
        if (val < 0x80) {
            res.push(val);
        }
        else if (val < 0x800) {
            res.push(0xC0 | ((val & 0x7C0) >> 6));
            res.push(0x80 |  (val & 0x03F)     );
        }
        else if (val < 0x10000) {
            res.push(0xE0 | ((val & 0xF000) >> 12));
            res.push(0x80 | ((val & 0x0FC0) >>  6));
            res.push(0x80 |  (val & 0x003F)      );
        }
        else if (val < 0x200000) {
            res.push(0xF0 | ((val & 0x1C0000) >> 18));
            res.push(0x80 | ((val & 0x03F000) >> 12));
            res.push(0x80 | ((val & 0x000FC0) >>  6));
            res.push(0x80 |  (val & 0x00003F)      );
        }
        else {
            res.push(63);  // '?'
        }
    }

    return res;
}
83384
public static byte[] longToBytes(long longValue) {
		// Magic number 8 should be defined as Long.SIZE / Byte.SIZE
		final byte[] result = new byte[8];
		for (int i = 7; i >= 0; i--) {
			result[i] = (byte) (longValue & 0xFF);
			longValue >>= 8;
		}
		return result;
	}
1012878
public static long byteArrayToLongBE( byte[] data ) {
        if (data == null)
            return 0x0;
        long accum = 0;
        int shiftBy = 8 * data.length - 8;
        for( byte b : data ) {
            accum |= (long) (0xff & b) << shiftBy;
            shiftBy -= 8;
        }
        return accum;
    }
41026
@VisibleForTesting
	static byte[] ascii8To6bitBin(byte[] toDecBytes) {

		byte[] convertedBytes = new byte[toDecBytes.length];
		int sum = 0;
		int _6bitBin = 0;

		for (int i = 0; i < toDecBytes.length; i++) {
			sum = 0;
			_6bitBin = 0;

			if (toDecBytes[i] < 48) {
				throw new AisParseException(AisParseException.INVALID_CHARACTER + " "
				        + (char) toDecBytes[i]);
			} else {
				if (toDecBytes[i] > 119) {
					throw new AisParseException(AisParseException.INVALID_CHARACTER + " "
					        + (char) toDecBytes[i]);
				} else {
					if (toDecBytes[i] > 87) {
						if (toDecBytes[i] < 96) {
							throw new AisParseException(AisParseException.INVALID_CHARACTER + " "
							        + (char) toDecBytes[i]);
						} else {
							sum = toDecBytes[i] + 40;
						}
					} else {
						sum = toDecBytes[i] + 40;
					}
					if (sum != 0) {
						if (sum > 128) {
							sum += 32;
						} else {
							sum += 40;
						}
						_6bitBin = sum & 0x3F;
						convertedBytes[i] = (byte) _6bitBin;
					}
				}
			}
		}

		return convertedBytes;
	}
701204
function unicodeStringToTypedArray(s) {
    var escstr = encodeURIComponent(s);
    var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode('0x' + p1);
    });
    var ua = new Uint8Array(binstr.length);
    Array.prototype.forEach.call(binstr, function (ch, i) {
        ua[i] = ch.charCodeAt(0);
    });
    return ua;
}
663759
public static byte[] toBytes(long value, int length) {
        if (length <= 0) return new byte[0];
        if (length > 8) length = 8;
        byte[] out = new byte[length];
        for (int i = length - 1; i >= 0; i--){
            out[i] = (byte)(value & 0xFFL);
            value = value >> 8;
        }
        return out;
    }
482236
private static byte[] intToByteArray(int value)
    {
        byte[] data = new byte[4];

        // int -> byte[]
        for (int i = 0; i < 4; ++i)
        {
            int shift = i << 3; // i * 8
            data[3 - i] = (byte) ((value & (0xff << shift)) >>> shift);
        }
        return data;
    }
1131456
function encode(data) {
  $.checkArgument(data instanceof Array, 'Must be Array');
  var base32 = '';
  for (var i=0; i<data.length; i++) {
    var value = data[i];
    $.checkArgument(0 <= value && value < 32, 'Non-base32, value ' + value);
    base32 += CHARSET[value];
  }
  return base32;
}
728638
public static long fromBytes(byte[] value){
        if (value == null) return 0;
        long out = 0;
        int length = value.length;
        if (length > 8) length = 8;
        for (int i = 0; i < length; i++){
            out = (out << 8) + (value[i] & 0xff);
        }
        return out;
    }
1051731
function encode2UTF8(charCode){
    if(charCode <= 0x7f){
      return [charCode];
    }else if(charCode <= 0x7ff){
      return [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)];
    }else{
      return [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)];
    }
  }