<%
/*
    $Revision$
    $Id$

    File:           encoding.inc
    Description:    Handige functie
    Parameters:
    Context:
    Note:
*/

function hex_to_binary(seed)
{
    var binary = new Array();
    for (var i=0; i<seed.length/2; i++)
    {
        var h = seed.substr(i*2, 2);
        binary[i] = parseInt(h,16);
    }
    return binary;
}

var base32_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
function binary_to_base32(input) {
  var ret = new Array();
  var ret_len = 0;
  var i = 0;

  var unpadded_length = input.length;
  while (input.length % 5) {
    input[input.length] = 0;
  }

	for(i=0; i<input.length; i+=5) {
		ret += base32_chars.charAt((input[i] >> 3));
		ret += base32_chars.charAt(((input[i] & 0x07) << 2) | ((input[i+1] & 0xc0) >> 6));
    if (i+1 >= unpadded_length) {
      ret += "======"
      break;
    }
		ret += base32_chars.charAt(((input[i+1] & 0x3e) >> 1));
		ret += base32_chars.charAt(((input[i+1] & 0x01) << 4) | ((input[i+2] & 0xf0) >> 4));
    if (i+2 >= unpadded_length) {
      ret += "===="
      break;
    }
		ret += base32_chars.charAt(((input[i+2] & 0x0f) << 1) | ((input[i+3] & 0x80) >> 7));
    if (i+3 >= unpadded_length) {
      ret += "==="
      break;
    }
		ret += base32_chars.charAt(((input[i+3] & 0x7c) >> 2));
		ret += base32_chars.charAt(((input[i+3] & 0x03) << 3) | ((input[i+4] & 0xe0) >> 5));
    if (i+4 >= unpadded_length) {
      ret += "="
      break;
    }
		ret += base32_chars.charAt(((input[i+4] & 0x1f)));
	}
	return ret;
}
%>