A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security. 

User Info  Posts  tryexcept  Base conversion of arbitrary sized numbers (PHP)  #1  I have a long "binary string" like the output of PHPs pack function. How can I convert this value to base62 (09azAZ)? The built in maths functions overflow with such long inputs, and BCmath doesn't have a base_convert function, or anything that specific. I would also need a matching "pack base62" function. posted date: 20081209 03:25:00


 kawhi  Re: Base conversion of arbitrary sized numbers (PHP)  #2  

 ruquay  Re: Base conversion of arbitrary sized numbers (PHP)  #3  Unless you really, really have to have base62, why not go for: base64_encode()base64_decode() The only other added characters are "+" and "=", and it's a very wellknown method to pack and unpack binary strings with available functions in many other languages. posted date: 20081212 17:26:00 

 Jay Dansand  Re: Base conversion of arbitrary sized numbers (PHP)  #4  I think there is a misunderstanding behind this questions. Base conversion and encoding/decoding are different. The output of base64_encode(...) is not a large base64number. It's a series of discrete base64 values, corresponding to the compression function. base64_encode(1234) = "MTIzNA=="base64_convert(1234) = "TS" //if the base64_convert function existed base64 encoding breaks the input up into groups of 3 bytes (24 bits), then converts each subsegment of 6 bits (2^6 = 64, which is the destination base) to the corresponding base64 character (values are "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", where A = 0, / = 63).In our example, "1234" becomes MTIZNA==, because (in ASCII) "1234" is 00110001 00110010 00110011 00110100 in binary. This gets broken into 001100 (M) 010011 (T) 001000 (I) 110011 (z) 001101 (N) 00. Since the last group isn't complete, it gets padded with 0's and the value is 000000 (A). Because everything is done by groups of 3 input characters, there are 2 groups: "123" and "4". The last group is padded with ='s to make it 3 chars long, so the whole output becomes "MTIZNA==".converting to base64, on the other hand, takes a single integer value and converts it into a single base64 value. For our example, 1234 (decimal) is "TS" (base64), if we use the same string of base64 values as above. Working backward, and lefttoright: T = 19 (column 1), S = 18 (column 0), so (19 * 64^1) + (18 * 64^0) = 19 * 64 + 18 = 1234 (decimal). posted date: 20091116 09:06:00 

 Core Xii  Re: Base conversion of arbitrary sized numbers (PHP)  #5  Here is a function base_conv() that can convert between completely arbitrary bases, expressed as arrays of strings; Each array element represents a single "digit" in that base, thus also allowing multicharacter values (it is your responsibility to avoid ambiguity). function base_conv($val, &$baseTo, &$baseFrom) { return base_arr_to_str(base_conv_arr(base_str_to_arr((string) $val, $baseFrom), count($baseTo), count($baseFrom)), $baseTo); }function base_conv_arr($val, $baseToDigits, $baseFromDigits) { $valCount = count($val); $result = array(); do { $divide = 0; $newlen = 0; for ($i = 0; $i < $valCount; ++$i) { $divide = $divide * $baseFromDigits + $val[$i]; if ($divide >= $baseToDigits) { $val[$newlen ++] = (int) ($divide / $baseToDigits); $divide = $divide % $baseToDigits; } else if ($newlen > 0) { $val[$newlen ++] = 0; } } $valCount = $newlen; array_unshift($result, $divide); } while ($newlen != 0); return $result; }function base_arr_to_str($arr, &$base) { $str = ''; foreach ($arr as $digit) { $str .= $base[$digit]; } return $str; }function base_str_to_arr($str, &$base) { $arr = array(); while ($str === '0'  !empty($str)) { foreach ($base as $index => $digit) { if (mb_substr($str, 0, $digitLen = mb_strlen($digit)) === $digit) { $arr[] = $index; $str = mb_substr($str, $digitLen); continue 2; } } throw new Exception(); } return $arr; } Examples: $baseDec = str_split('0123456789');$baseHex = str_split('0123456789abcdef');echo base_conv(255, $baseHex, $baseDec); // ffecho base_conv('ff', $baseDec, $baseHex); // 255// multicharacter base:$baseHelloworld = array('hello ', 'world ');echo base_conv(37, $baseHelloworld, $baseDec); // world hello hello world hello world echo base_conv('world hello hello world hello world ', $baseDec, $baseHelloworld); // 37// ambiguous base:// don't do this! base_str_to_arr() won't know how to decode e.g. '11111'// (well it does, but the result might not be what you'd expect;// It matches digits sequentially so '11111' would be array(0, 0, 1)// here (matched as '11', '11', '1' since they come first in the array))$baseAmbiguous = array('11', '1', '111'); posted date: 20110130 23:28:00 

 select page: « 1 » 

