note 12168 modified in function.base-convert by danbrown

From: Date: Sat, 02 Jul 2011 15:42:10 +0000
Subject: note 12168 modified in function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181177@lists.php.net to get a copy of this message
if your worried about little/big edian affecting your dec to binary conversions, or just want to convert large numbers then try this bit of code: <?php // this little bit of code is simply to convert a 64bit number. function convert_num ($value) { $value = (string) $value; while (!preg_match('/^0*$/', $value) && $j < 65) { $cumulate = ''; $rem = ''; for ($i = 0; $i < strlen($value); $i++) { if ($cumulate == '') { if (floor(($value[$i] + $rem) / 2)) { $cumulate .= floor(($value[$i] + $rem) / 2); } } else { $cumulate .= floor(($value[$i] + $rem) / 2); } $rem = $value [$i] % 2 * 10; } $r = floor($rem / 10); $re .= $r; $value = $cumulate; $j++; } return $re; } print convert_num (65535); ?> --was-- if your worried about little/big edian affecting your dec to binary conversions, or just want to convert large numbers then try this bit of code: <? // this little bit of code is simply to convert a 64bit number. function convert_num ($value) { $value = (string) $value; while (!preg_match('/^0*$/', $value) && $j < 65) { $cumulate = ''; $rem = ''; for ($i = 0; $i < strlen($value); $i++) { if ($cumulate == '') { if (floor(($value[$i] + $rem) / 2)) { $cumulate .= floor(($value[$i] + $rem) / 2); } } else { $cumulate .= floor(($value[$i] + $rem) / 2); } $rem = $value [$i] % 2 * 10; } $r = floor($rem / 10); $re .= $r; $value = $cumulate; $j++; } return $re; } print convert_num (65535); ?> http://php.net/manual/en/function.base-convert.php

« previous php.notes (#181177) next »