function copyToArray(s,a,start,max) {
		if (max===undefined) max=s.length;
		if (start===undefined) start=0;
		var t=toType(s);
		
		if (t=="arraybuffer") {
			s=new Uint8Array(s);
			t='uint8array';
		}

		switch (t) {
		  case 'array':
		  case 'uint8array':
		  	//for (var i=0; i<max;i++)
			//      a[i+start]=s[i];
		  	// not much of an improvement using .set(), if any at all
			if (max>=s.length) {
				a.set(s,start);
				return s.length;
			} else if (t=='array') {
				a.set(s.slice(0,max),start);
			} else {
				a.set(s.subarray(0,max),start);
			}
			return max;
		  case 'string':
		  	for (var i=0; i<max;i++) 
				a[i+start]=s.charCodeAt(i);
			return i;
		  default:
		  	return 0;
		}
	}
/* copy string or uint8array into a uint8array 
		start is where in output array to start copying
		max is max bytes to copy from input string/array
		
		does not check if a is big enough to take s
	*/
copy string or uint8array into a uint8array
start is where in output array to start copying
max is max bytes to copy from input string/array

does not check if a is big enough to take s
copy string or uint8array into a uint8array start is where in output array to start copying max is max bytes to copy from input string / array does not check if a is big enough to take s
function copyToArray(s,a,start,max) {
		if (max===undefined) max=s.length;
		if (start===undefined) start=0;
		var t=toType(s);
		if (t=="arraybuffer") {
			s=new Uint8Array(s);
			t='uint8array';
		}
		switch (t) {
		  case 'array':
		  case 'uint8array':
			if (max>=s.length) {
				a.set(s,start);
				return s.length;
			} else if (t=='array') {
				a.set(s.slice(0,max),start);
			} else {
				a.set(s.subarray(0,max),start);
			}
			return max;
		  case 'string':
		  	for (var i=0; i<max;i++) 
				a[i+start]=s.charCodeAt(i);
			return i;
		  default:
		  	return 0;
		}
	}
function copyToArray ( s , a , start , max ) { if ( max === undefined ) max = s . length ; if ( start === undefined ) start = 0 ; var t = toType ( s ) ; if ( t == "arraybuffer" ) { s = new Uint8Array ( s ) ; t = 'uint8array' ; } switch ( t ) { case 'array' : case 'uint8array' : if ( max >= s . length ) { a . set ( s , start ) ; return s . length ; } else if ( t == 'array' ) { a . set ( s . slice ( 0 , max ) , start ) ; } else { a . set ( s . subarray ( 0 , max ) , start ) ; } return max ; case 'string' : for ( var i = 0 ; i < max ; i ++ ) a [ i + start ] = s . charCodeAt ( i ) ; return i ; default : return 0 ; } }
copy string or uint8array into a uint8array
start is where in output array to start copying
max is max bytes to copy from input string/array
copy string or uint8array into a uint8array start is where in output array to start copying max is max bytes to copy from input string / array
//for (var i=0; i<max;i++) //      a[i+start]=s[i]; // not much of an improvement using .set(), if any at all