The Bytes parameter specifies how many bytes each encoded string character should use. The most efficient seems to be using only a single byte. -1 is useful for the PHP Implementation if a HTTP Request returns using the charset ISO-8859-1 to encode in a better ratio.

PHP Implementation

<?phpdefine("TYPE_INTEGER",1);define("TYPE_FLOAT",2);define("TYPE_STRING",3);define("TYPE_KEY",4);define("TYPE_VECTOR",5);define("TYPE_ROTATION",6);$Unusable=array(array(0x0000,1),array(0x000D,1),array(0x007C,1),array(0x007E,1),array(0xD800,2047),array(0xFDD0,31),array(0xFFFE,2));//function unichr($u) {// return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');//}function mb_str_split($String){$stop=mb_strlen($String);$result=array();for($idx=0;$idx<$stop;$idx++)$result[]=mb_substr($String,$idx,1);return$result;}function IntToBaseToUTF8($Int,$Base){global$Unusable;$Base=(int)$Base;// Integer to Base$HalfBase=(int)floor($Base/2);$Sign=$Int<0;if($Sign)$Int=-$Int;$M=$Int%$HalfBase;$Coded=array($M+$HalfBase*$Sign);$Int=(int)($Int/$HalfBase);while($Int>$Base){array_unshift($Coded,$Int%$Base);$Int=(int)($Int/$Base);}if($Int)array_unshift($Coded,$Int);// Base to UTF8$Enc=array();$Unusables=count($Unusable);foreach($Codedas$Val){$Int=$Val;for($i=0;$i<$Unusables;++$i){if($Int>=$Unusable[$i][0])$Int+=$Unusable[$i][1];else$i=$Unusables;}$IntArr=array($Int);$Str= UnicodeToUTF8($IntArr);//<- Handles multi-octets, old: unichr($Int); $Enc[]=$Str;}if($Base>60000)returnimplode('|',$Enc);elsereturnimplode('',$Enc);}function UTF8ToBaseToInt($UTF8,$Base){global$Unusable;$Base=(int)$Base;$Dec=array();if(Base >60000)$Dec=mb_split('/\|/',$UTF8);else$Dec= mb_str_split($UTF8);// UTF8 to Base$Int=0;$Coded=array();foreach($Decas$Char){list($Int)= UTF8ToUnicode($Char);$Unusables=count($Unusable);for($i=$Unusables-1;$i>=0;--$i){if($Int>=$Unusable[$i][0])$Int-=$Unusable[$i][1];}$Coded[]=$Int;}// Base to Int$k=count($Coded);$Exp=(int)floor($Base/2);$Int=$Coded[--$k];if($Int>$Exp){$Sign=1;$Int-=$Exp;}else$Sign=0;while($k--){$Int+=$Coded[$k]*$Exp;$Exp*=$Base;}if($Sign)$Int=-$Int;return$Int;}function BaseN_Enc($Bytes,$Input){if($Bytes==1)$Base=123;elseif($Bytes==2)$Base=2043;elseif($Bytes==3)$Base=1112025;elseif($Bytes==-1)$Base=251;// Use for web transport with "charset=ISO-8859-1"$Compressed=array();$Types=array();for($x=0,$y=count($Input);$x<$y;++$x){$Var=$Input[$x];// Get as Integer(s) from Input array, and compress into Compressed arrayif(is_int($Var)){$Compressed[]= IntToBaseToUTF8($Var,$Base);$Type= TYPE_INTEGER;}elseif(is_float($Var)){$Compressed[]= IntToBaseToUTF8( fui($Var),$Base);$Type= TYPE_FLOAT;}elseif( IsKey($Var)){$Var=str_replace('-','',$Var);$Compressed[]= IntToBaseToUTF8((int)('0x'.substr($Var,0,8)),$Base);$Compressed[]= IntToBaseToUTF8((int)('0x'.substr($Var,8,8)),$Base);$Compressed[]= IntToBaseToUTF8((int)('0x'.substr($Var,16,8)),$Base);$Compressed[]= IntToBaseToUTF8((int)('0x'.substr($Var,24,8)),$Base);$Type= TYPE_KEY;}elseif(($Array= IsVecOrRot($Var))!==FALSE){foreach($Arrayas$Val)$Compressed[]= IntToBaseToUTF8( fui($Val),$Base);if(count($Array)==3)$Type= TYPE_VECTOR;else$Type= TYPE_ROTATION;}elseif(is_string($Var)){// May be a conflict if string contains any delimiter characters '~', '|'$Compressed[]=$Var;$Type= TYPE_STRING;}// Add to header$Row=$x%10;if(!$Row)$Types[]= Type;else{$Col=$x/10;$t=$Types[$Col];$t|=$Type<<($Row*3);$Types[$Col]=$t;}}// Compress header$y=count($Types=array_merge((array)$y,$Types));for($x=0;$x<$y;++$x){$Types[$x]= IntToBaseToUTF8($Types[$x],$Base);}returnimplode('~',array_merge($Types,$Compressed));}function BaseN_Dec($Bytes,$Encrypted){if($Bytes==1)$Base=123;elseif($Bytes==2)$Base=2043;elseif($Bytes==3)$Base=1112025;elseif($Bytes==-1)$Base=251;// Use for web transport with "charset=ISO-8859-1"$Input=explode('~',$Encrypted);$Total= UTF8ToBaseToInt($Input[0],$Base);$Types=array();for($x=0,$y=1+($Total/10);$x<$y;++$x)$Types[]= UTF8ToBaseToInt($Input[$x+1],$Base);$Output=array();$Ptr=$y;for($x=0;$x<$Total;++$x){$Row=$x%10;$Col=$x/10;$Type=($Types[$Col]>>($Row*3))&7;if($Type== TYPE_INTEGER){$Output[]= UTF8ToBaseToInt($Input[$Ptr++],$Base);}elseif($Type== TYPE_FLOAT){$Output[]= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));}elseif($Type== TYPE_KEY){$Int0= UTF8ToBaseToInt($Input[$Ptr++],$Base);$Int1= UTF8ToBaseToInt($Input[$Ptr++],$Base);$Int2= UTF8ToBaseToInt($Input[$Ptr++],$Base);$Int3= UTF8ToBaseToInt($Input[$Ptr++],$Base);$Output[]= Ints2Key($Int0,$Int1,$Int2,$Int3);}elseif($Type== TYPE_VECTOR){$VX= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$VY= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$VZ= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$Output[]= Floats2Vec($VX,$VY,$VZ);}elseif($Type== TYPE_ROTATION){$RX= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$RY= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$RZ= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$RS= iuf( UTF8ToBaseToInt($Input[$Ptr++],$Base));$Output[]= Floats2Rot($RX,$RY,$RZ,$RS);}elseif($Type== TYPE_STRING){$Output[]=$Input[$Ptr++];}}return$Output;}function IsKey($UUID){returnpreg_match('@[[:xdigit:]-]{36}@',$UUID);}function IsVecOrRot($Input){$Total=preg_match_all('/-?[0-9]*\.[0-9]+|[0-9]+/',$Input,$Matches);if($Total==3||$Total==4)return$Matches;elsereturnFALSE;}function Ints2Key($a,$b,$c,$d){returnsprintf('%x%x-%x-%x-%x-%x%x%x',($a>>16)&0xFFFF,$a&0xFFFF,($b>>16)&0xFFFF,$b&0xFFFF,($c>>16)&0xFFFF,$c&0xFFFF,($d>>16)&0xFFFF,$d&0xFFFF);}function Floats2Vec($x,$y,$z){returnsprintf('<%F, %F, %F>',$x,$y,$z);}function Floats2Rot($x,$y,$z,$s){returnsprintf('<%F, %F, %F, %F>',$x,$y,$z,$s);}function fui($float){// Port of Strifes' functionif(($float)!=0.0){$sign=(int)($float<0)<<31;if(($float=abs($float))<2.3509887016445750159374730744445e-38)return$sign|(int)($float/1.4012984643248170709237295832899e-45);if($float>3.4028234663852885981170418348452e+38)return$sign|0x7F800000;$exp=(int)floor((log($float)/0.69314718055994530941723212145818));$float/=pow(2.0,$exp-=(($exp>>31)|1));$d=(int)$float-(3<=$float);return(0x7FFFFF&(int)($float*(0x1000000>>$d)))|((($exp+126+$d)<<23)|$sign);}return(int)((string)$float==(string)(-0.0))<<31;}function iuf($int){// Port of Strifes' functionif(($int&0x7FFFFFFF)==0x7F800000)//Infinity Checkreturn(($int>>31)|1)/0.0;$exp=($int>>23)&0xFF;returnsprintf('%.6F',pow(2.0,($exp|!$exp)-150)*(((!!$exp)<<23)|(($int&0x7FFFFF)))*(1|($int>>31)));}/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen, hsivonen@iki.fi
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** *//*
* For the original C++ code, see
* http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUTF8ToUnicode.cpp
* http://lxr.mozilla.org/seamonkey/source/intl/uconv/src/nsUnicodeToUTF8.cpp
*
* The latest version of this file can be obtained from
* http://iki.fi/hsivonen/php-utf8/
*
* Version 1.0, 2003-05-30
*//**
* Takes an UTF-8 string and returns an array of ints representing the
* Unicode characters. Astral planes are supported ie. the ints in the
* output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
* are not allowed.
*
* Returns false if the input string isn't a valid UTF-8 octet sequence.
*/function UTF8ToUnicode(&$str){$mState=0;// cached expected number of octets after the current octet// until the beginning of the next UTF8 character sequence$mUcs4=0;// cached Unicode character$mBytes=1;// cached expected number of octets in the current sequence$out=array();$len=strlen($str);for($i=0;$i<$len;$i++){$in=ord($str{$i});if(0==$mState){// When mState is zero we expect either a US-ASCII character or a// multi-octet sequence.if(0==(0x80&($in))){// US-ASCII, pass straight through.$out[]=$in;$mBytes=1;}elseif(0xC0==(0xE0&($in))){// First octet of 2 octet sequence$mUcs4=($in);$mUcs4=($mUcs4&0x1F)<<6;$mState=1;$mBytes=2;}elseif(0xE0==(0xF0&($in))){// First octet of 3 octet sequence$mUcs4=($in);$mUcs4=($mUcs4&0x0F)<<12;$mState=2;$mBytes=3;}elseif(0xF0==(0xF8&($in))){// First octet of 4 octet sequence$mUcs4=($in);$mUcs4=($mUcs4&0x07)<<18;$mState=3;$mBytes=4;}elseif(0xF8==(0xFC&($in))){/* First octet of 5 octet sequence.
*
* This is illegal because the encoded codepoint must be either
* (a) not the shortest form or
* (b) outside the Unicode range of 0-0x10FFFF.
* Rather than trying to resynchronize, we will carry on until the end
* of the sequence and let the later error handling code catch it.
*/$mUcs4=($in);$mUcs4=($mUcs4&0x03)<<24;$mState=4;$mBytes=5;}elseif(0xFC==(0xFE&($in))){// First octet of 6 octet sequence, see comments for 5 octet sequence.$mUcs4=($in);$mUcs4=($mUcs4&1)<<30;$mState=5;$mBytes=6;}else{/* Current octet is neither in the US-ASCII range nor a legal first
* octet of a multi-octet sequence.
*/returnfalse;}}else{// When mState is non-zero, we expect a continuation of the multi-octet// sequenceif(0x80==(0xC0&($in))){// Legal continuation.$shift=($mState-1)*6;$tmp=$in;$tmp=($tmp&0x0000003F)<<$shift;$mUcs4|=$tmp;if(0==--$mState){/* End of the multi-octet sequence. mUcs4 now contains the final
* Unicode codepoint to be output
*
* Check for illegal sequences and codepoints.
*/// From Unicode 3.1, non-shortest form is illegalif(((2==$mBytes)&&($mUcs4<0x0080))||((3==$mBytes)&&($mUcs4<0x0800))||((4==$mBytes)&&($mUcs4<0x10000))||(4<$mBytes)||// From Unicode 3.2, surrogate characters are illegal(($mUcs4&0xFFFFF800)==0xD800)||// Codepoints outside the Unicode range are illegal($mUcs4>0x10FFFF)){returnfalse;}if(0xFEFF!=$mUcs4){// BOM is legal but we don't want to output it$out[]=$mUcs4;}//initialize UTF8 cache$mState=0;$mUcs4=0;$mBytes=1;}}else{/* ((0xC0 & (*in) != 0x80) && (mState != 0))
*
* Incomplete multi-octet sequence.
*/returnfalse;}}}return$out;}/**
* Takes an array of ints representing the Unicode characters and returns
* a UTF-8 string. Astral planes are supported ie. the ints in the
* input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates
* are not allowed.
*
* Returns false if the input array contains ints that represent
* surrogates or are outside the Unicode range.
*/function UnicodeToUTF8(&$arr){$dest='';foreach($arras$src){if($src<0){returnfalse;}elseif($src<=0x007f){$dest.=chr($src);}elseif($src<=0x07ff){$dest.=chr(0xc0|($src>>6));$dest.=chr(0x80|($src&0x003f));}elseif($src==0xFEFF){// nop -- zap the BOM}elseif($src>=0xD800&&$src<=0xDFFF){// found a surrogatereturnfalse;}elseif($src<=0xffff){$dest.=chr(0xe0|($src>>12));$dest.=chr(0x80|(($src>>6)&0x003f));$dest.=chr(0x80|($src&0x003f));}elseif($src<=0x10ffff){$dest.=chr(0xf0|($src>>18));$dest.=chr(0x80|(($src>>12)&0x3f));$dest.=chr(0x80|(($src>>6)&0x3f));$dest.=chr(0x80|($src&0x3f));}else{// out of rangereturnfalse;}}return$dest;}?>