/*
Copyright (C) 2004-2009 Stewart Gordon.

This software is provided 'as-is', without any express or implied
warranty.  In no event will the author be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute
it freely, subject to the following restrictions:

- The origin of this software must not be misrepresented; you must
  not claim that you wrote the original software.  If you use this
  software in a product, an acknowledgment in the product
  documentation would be appreciated but is not required.
- Altered source versions must be plainly marked as such, and must
  not be misrepresented as being the original software.
- This notice may not be removed or altered from any source
  distribution.
*/
var base = 16;
var digits = '0123456789ABCDEF';

function strToNumbers(str, max) {
	var numbers = new Array;
	str = str.toUpperCase().split(' ');
	for (i in str) {
		if (str[i] != '') numbers[numbers.length] = strToNumber(str[i], max);
	}
	return numbers;
}

function strToNumber(str, max) {
	var number = 0;
	for (i in str) {
		var d = digits.indexOf(str[i]);
		if (d < 0) throw 'non-number entered';
		number = number * base + d;
		if (number > max) throw 'number out of range';
	}
	return number;
}

function numbersToStr(numbers, minRange) {
	var str = '';
	for (i in numbers) {
		str += numberToStr(numbers[i], minRange) + ' ';
	}
	return str;
}

function numberToStr(number, minRange) {
	if (number == -1) return base == 2 ? '--------' : base == 10 ? '---' : '--';
	
	if (base == 10) return number.toString();
	
	var str = '';
	var range = 1;
	while (number > 0 || range < minRange) {
		str = digits.charAt(number % base) + str;
		number = Math.floor(number / base);
		range *= base;
	}
	return str;
}

