Scripts

integer 2 base64

Kleine simpele class om een getal om te zetten in BASE64. Zie comments voor de werking. Werkt prima in combinatie met: http://www.phphulp.nl/php/scripts/11/1568/ sterker nog, ze zijn voor elkaar gemaakt :P De class is getuned voor high-speed. Heb extra tijd besteed voor high-performance. Te gebruiken in static context. Voorbeeld: $string = base64::int(123456); echo base64::int($string);//geeft mijn getalletje weer t'rug. Functie "base64::multiint()" toegevoegd. De functie is afhankelijk van de "multiint" functie snipper: http://www.phphulp.nl/php/scripts/11/1568/ Met deze functie kan je meerdere integers/booleans opslaan in BASE64. iedere 6 bits aan data (6 booleans bijv., of 2 booleans en 1 getal van 0-15) kost 1 teken/byte aan data. De functie werkt het beste met een meervoud van 6 bits aan data, met een maximum van 30 bits aan data. (6, 12, 18, 24 of 30 bits aan data) LET OP: functie "base64::multiint()" is afhankelijk van http://www.phphulp.nl/php/scripts/11/1568/ Bekijk de documentatie van dat snippertje voor de werking van de functie. Voorbeeld van gebruik van de functie:

integer-2-base64
[code]<?
class base64 {
	public static $BASE64ENC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
	public static $BASE64DEC = array();
	
	/*
	Function to encode or decode an INT to/from BASE64.
	INPUT:
		$int: MIXED representing an INT or a STRING. 
			When an INT is given, it will be encoded to base64.
			When a string is given, it will be decoded as base64.
		$length: INT representing the fixed length of the base64 output. Only used when an INT is given as first argument.
	*/
	public function int($target,$length = 0) {
		if(is_string($target)) {
			return self::int_decode($target);
		} elseif(is_int($target)) {
			return self::int_encode($target,$length);
		}
	}
	
	/*
	Function to encode/decode multiple integers based on the multi-int function.
	INPUT:
		$target: MIXED representing an INT or an ARRAY.
			When an INT is given, it will be extracted to multiple ints. OUTPUT will be an ARRAY containing the integers.
			When an ARRAY is given, it will be stored to a single int. OUTPUT will be an INT.
		$layout: ARRAY containing multiple integers representing the reserved amount of bits per integer.

	OUTPUT:
		MIXED: ARRAY or INT.
	
	NOTES:
		-It's recommended to store a multiply of 6 bits. (6, 12, 18, 24 or 30 bits)
		-Try not to store more then 30 bits of data.
		-For every 6 bits, a single BASE64 character will be returned.
	*/
	public function multiint($target,$layout) {
		if(is_string($target)) {
			return multiint(self::int_decode($target),$layout);
		} elseif(is_array($target)) {
			$length = ceil(array_sum($layout)/6);
			return self::int_encode(multiint($target,$layout),$length);			
		}
	}
	
	/*
	Function to encode an int to a BASE64 string.
	Input:
		$int: 	 INT representing the integer to encode.
		$length: INT respresenting the fixed length of the BASE64 output.
		
	Output:
		STRING representing the BASE64 encoded int.
	*/
	public function int_encode($int,$length = 0) {
		$BASE64ENC = self::$BASE64ENC;
		$output = '';
		$power = 1;
		do {
			$power *= 64;
			$substract = $int%$power;
			$output = $BASE64ENC[(int)($substract/($power/64))] . $output;
		} while($int/$power >= 1);
		
		if($length) {
			$output = substr(str_pad($output,$length,$BASE64ENC[0],STR_PAD_LEFT),-$length);
		}
		
		return $output;
	}

	/*
	Function to decode an int to a BASE64 string.
	Input:
		$base64: STRING representing the BASE64 encoded int.
		
	Output:
		INT representing the decoded int.
	*/
	public function int_decode($base64) {
		$BASE64DEC = self::$BASE64DEC;
		$base64 = strrev((string)$base64);
		$int=0;
		$power = 1;
		for($x=0;$x<strlen($base64);$x++) {
			$int += $power*$BASE64DEC[$base64[$x]];
			$power *= 64;
		}
		return $int;
	}
}
base64::$BASE64DEC = array_flip(str_split(base64::$BASE64ENC));
?>[/code]

Reacties

0
Nog geen reacties.