In response to "Ray Paseur sometimes uses GMail"
There is part of my class for specified operations on string etc.
I've written two methods to convert numbers between roman and arabic notation:
<?php
class Text
{
public function Arab2Roman($number)
{
$out = '';
$translate = array(
array('arab' => 5000, 'roman' => ''),
array('arab' => 1000, 'roman' => 'M'),
array('arab' => 900, 'roman' => 'CM'),
array('arab' => 500, 'roman' => 'D'),
array('arab' => 400, 'roman' => 'CD'),
array('arab' => 100, 'roman' => 'C'),
array('arab' => 90, 'roman' => 'XC'),
array('arab' => 50, 'roman' => 'L'),
array('arab' => 40, 'roman' => 'XL'),
array('arab' => 10, 'roman' => 'X'),
array('arab' => 9, 'roman' => 'IX'),
array('arab' => 5, 'roman' => 'V'),
array('arab' => 4, 'roman' => 'IV'),
array('arab' => 1, 'roman' => 'I')
);
if($number > $translate[0]['arab'])
$out = false;
else
{
$j = count($translate)-1;
for($i=0;$i<$j;$i++)
{
if($number < $translate[$i]['arab'] && $number >= $translate[$i+1]['arab'])
{
$out .= $translate[$i+1]['roman'];
$number -= $translate[$i+1]['arab'];
if($number)
$out .= Text::Arab2Roman($number); // do it recursive until $numer is not 0
break;
}
}
}
return ($out);
}
public function Roman2Arab($roman)
{
$out = 0;
$translate = array(
'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
'I' => 1
);
$j = strlen($roman);
for($i=0; $i<$j; $i++)
{
$out += $translate[$roman[$i]];
// find wrong order of roman chars
if($i < $j-1 && $translate[$roman[$i]] < $translate[$roman[$i+1]])
{
$out -= 2*$translate[$roman[$i]];
}
}
return ($out);
}
}
?>
base_convert
(PHP 4, PHP 5)
base_convert — 수를 임의의 진수로 변환합니다.
설명
string base_convert
( string $number
, int $frombase
, int $tobase
)
tobase 진수로 표현한 number 를 포함하는 문자열을 반환합니다. number 의 진수는 frombase 로 주어집니다. frombase 와 tobase 는 2와 36(포함) 사이의 값이여야 합니다. 10 이상의 진수로 표현되는 자리수는 a-z로 표현되며, a는 10, b는 11, ..., z는 35를 의미합니다.
Example#1 base_convert() 예제
<?php
$hexadecimal = 'A37334';
echo base_convert($hexadecimal, 16, 2);
?>
출력:
101000110111001100110100
Warning
base_convert()는 큰 수에 대해서는 내부적으로 "double"이나 "float" 형을 사용하기 때문에, 부정확할 수 있습니다. 더욱 정확한 정보와 제한에 대해서는 부동소수점수 섹션을 참고하십시오.
base_convert
mr-bungle sometimes uses TLEN.PL
06-Aug-2008 04:38
06-Aug-2008 04:38
dorantor
13-May-2008 10:42
13-May-2008 10:42
Spent almost a day to find a bug(?) in base_convert() function when converting big values to binary:
<?php
$value = 2919739656537;
echo base_convert($value, 10, 2);
?>
should print
101010011111001110000010111000100101011001
in reality, prints
10101000111000001100011100110101110110100100
but that is wrong!
so, in result, I've written this two functions:
<?php
/**
* Convert binary to decimal
* works with big numbers
*
* @param string $binary
* @return float
*/
function big_bin2dec($binary)
{
$result = (float)0;
$shift = 0;
while ( 0 < strlen($binary) ) {
$bit = substr($binary, -1);
$binary = substr($binary, 0, -1);
$result = $result + $bit*pow(2, $shift);
++$shift;
}
return $result;
}
/**
* convert decimal 2 binary presentation
*
* @param mixed $dec
* @return string
*/
function big_dec2bin($dec)
{
$result = '';
$shift = 0;
while ( pow(2, $shift) < $dec ) {
++$shift;
}
while ( 0 <= $shift ) {
$pow = pow(2, $shift);
if ( $pow <= $dec ) {
$dec-= $pow;
$result = $result . '1';
} else {
$result = $result . '0';
}
--$shift;
}
return $result;
}
?>
Functions tested only with positive integers(that's enough for me), so if you need more than this you'll, possibly, have to rework 'em.
Simple test:
<?php
$value = pow(2, 48);//only one bit should be set
printf("%f \n", $value);
echo base_convert($value, 10, 2), "\n";
echo big_dec2bin($value), "\n";
?>
prints out something similar to:
281474976710656.000000
110011001100110011001100110011001100111001010
1000000000000000000000000000000000000000000000000
I hope this will help someone :)
BTW! My phpinfo() says PHP Version 5.2.1
Todd Stokes @ Georgia Tech
20-Nov-2007 04:37
20-Nov-2007 04:37
I wrote a function to fix the problem stated above about floating point numbers. (this is especially useful for arithmetic coding, which is what I'm working on) It's not tremendously robust, but it gets the job done for "simple" float values.
function floatbin($myfloat, $precision=16, $return_int_part=true) {
//echo "complete: $myfloat\n";
$binary_string = "";
if($return_int_part) {
$binary_string = decbin($myfloat);
$binary_string .= ".";
$fractional_part = $myfloat-intval($myfloat);
} else {
$fractional_part = explode(".",$myfloat);
$fractional_part = ".".$fractional_part[1];
}
//echo "fractional: $fractional_part\n";
for ($i=1; $i < $precision+1; $i++) {
$binary_fraction = 1/pow(2,$i);
if($fractional_part >= $binary_fraction) {
$binary_string .= "1";
$fractional_part = $fractional_part - $binary_fraction;
} else {
$binary_string .= "0";
}
if($fractional_part == 0) {
break;
}
//echo "order: $binary_fraction\t\t";
//echo "state: $fractional_part\n";
}
return $binary_string;
}
// no problem
$myfloat = 1.5;
$decfloat = base_convert($myfloat,10,2);
echo "result: $decfloat\n"; // returns 1111 (wrong)
$decfloat = floatbin($myfloat, 32);
echo "result: $decfloat\n"; // returns 1.1 (correct)
$decfloat = floatbin($myfloat,16,false);
echo "result: $decfloat\n"; // returns 1 (correct)
// no problem
$myfloat = 4.125;
$decfloat = base_convert($myfloat,10,2);
echo "result: $decfloat\n"; // returns 1000000011101 (wrong)
$decfloat = floatbin($myfloat, 32);
echo "result: $decfloat\n"; // returns 100.001 (correct)
$decfloat = floatbin($myfloat,16,false);
echo "result: $decfloat\n"; // returns 001 (correct)
// will be truncated, so the inverse will not be ==
$myfloat = 12.3456;
$decfloat = base_convert($myfloat,10,2);
echo "result: $decfloat\n"; // returns 11110001001000000 (wrong)
$decfloat = floatbin($myfloat, 32);
echo "result: $decfloat\n"; // returns 1100.01011000011110010011110111011001 (correct, truncated)
$decfloat = floatbin($myfloat,16,false);
echo "result: $decfloat\n"; // returns 0101100001111001 (correct, truncated more)
// a case on where passing false is useful, so the inverse will not be ==
$myfloat = 777777777777.777777;
$decfloat = base_convert($myfloat,10,2);
echo "result: $decfloat\n"; // wrong
$decfloat = floatbin($myfloat, 32);
echo "result: $decfloat\n"; // wrong
$decfloat = floatbin($myfloat,16,false);
echo "result: $decfloat\n"; // correct, but truncated
CFK
14-Feb-2007 05:07
14-Feb-2007 05:07
Here is the function made by David Leppek who works in both way :
<?php
function big_convert ($number,$to_base) {
$result = "";
switch ($to_base) {
case "2":
$temp = preg_split('//', $number, -1, PREG_SPLIT_DELIM_CAPTURE);
$lng = strlen($number);
for ($i = 1;$i <= $lng;$i++) { $result .= str_pad(base_convert($temp[$i], 16, 2), 4, '0', STR_PAD_LEFT); }
return $result;
break;
case "16":
$bin = substr(chunk_split (strrev($number), 4,'-'), 0, -1);
$temp = preg_split('[-]', $bin, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = count($temp)-1;$i >= 0;$i--) { $result = $result . base_convert(strrev($temp[$i]), 2, 16); }
return strtoupper($result);
break;
}
}
$bin = "1011010011110010100100101011011101100101001101001111";
echo $hex = big_convert ($bin,16);
echo big_convert ($hex,2);
?>
Ray Paseur sometimes uses GMail
06-Dec-2006 03:55
06-Dec-2006 03:55
function roman_numerals($input_arabic_numeral='') {
if ($input_arabic_numeral == '') { $input_arabic_numeral = date("Y"); } // DEFAULT OUTPUT: THIS YEAR
$arabic_numeral = intval($input_arabic_numeral);
$arabic_numeral_text = "$arabic_numeral";
$arabic_numeral_length = strlen($arabic_numeral_text);
if (!ereg('[0-9]', $arabic_numeral_text)) {
return false; }
if ($arabic_numeral > 4999) {
return false; }
if ($arabic_numeral < 1) {
return false; }
if ($arabic_numeral_length > 4) {
return false; }
$roman_numeral_units = $roman_numeral_tens = $roman_numeral_hundreds = $roman_numeral_thousands = array();
$roman_numeral_units[0] = $roman_numeral_tens[0] = $roman_numeral_hundreds[0] = $roman_numeral_thousands[0] = ''; // NO ZEROS IN ROMAN NUMERALS
$roman_numeral_units[1]='I';
$roman_numeral_units[2]='II';
$roman_numeral_units[3]='III';
$roman_numeral_units[4]='IV';
$roman_numeral_units[5]='V';
$roman_numeral_units[6]='VI';
$roman_numeral_units[7]='VII';
$roman_numeral_units[8]='VIII';
$roman_numeral_units[9]='IX';
$roman_numeral_tens[1]='X';
$roman_numeral_tens[2]='XX';
$roman_numeral_tens[3]='XXX';
$roman_numeral_tens[4]='XL';
$roman_numeral_tens[5]='L';
$roman_numeral_tens[6]='LX';
$roman_numeral_tens[7]='LXX';
$roman_numeral_tens[8]='LXXX';
$roman_numeral_tens[9]='XC';
$roman_numeral_hundreds[1]='C';
$roman_numeral_hundreds[2]='CC';
$roman_numeral_hundreds[3]='CCC';
$roman_numeral_hundreds[4]='CD';
$roman_numeral_hundreds[5]='D';
$roman_numeral_hundreds[6]='DC';
$roman_numeral_hundreds[7]='DCC';
$roman_numeral_hundreds[8]='DCCC';
$roman_numeral_hundreds[9]='CM';
$roman_numeral_thousands[1]='M';
$roman_numeral_thousands[2]='MM';
$roman_numeral_thousands[3]='MMM';
$roman_numeral_thousands[4]='MMMM';
if ($arabic_numeral_length == 3) { $arabic_numeral_text = "0" . $arabic_numeral_text; }
if ($arabic_numeral_length == 2) { $arabic_numeral_text = "00" . $arabic_numeral_text; }
if ($arabic_numeral_length == 1) { $arabic_numeral_text = "000" . $arabic_numeral_text; }
$anu = substr($arabic_numeral_text, 3, 1);
$anx = substr($arabic_numeral_text, 2, 1);
$anc = substr($arabic_numeral_text, 1, 1);
$anm = substr($arabic_numeral_text, 0, 1);
$roman_numeral_text = $roman_numeral_thousands[$anm] . $roman_numeral_hundreds[$anc] . $roman_numeral_tens[$anx] . $roman_numeral_units[$anu];
return ($roman_numeral_text);
}
Kiam
12-Aug-2006 05:07
12-Aug-2006 05:07
Refering to the function posted by Michael Renner, I would change the line
$result = $tostring{$divide} . $result;
in
$result = $chars{$divide} . $result;
as $divide seems garanted to be less than $tobase.
That also eliminates the need of the variable $tostring.
-- Kiam
Michael Renner
17-May-2006 06:24
17-May-2006 06:24
Here is an unfucked version of the arbitrary-large-number base_convert examples below:
I modified it so that it works as drop-in replacement for base_convert. Attention, no sanity checking is done for the input numbers, anything larger than 36 won't work..
function unfucked_base_convert ($numstring, $frombase, $tobase) {
$chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$tostring = substr($chars, 0, $tobase);
$length = strlen($numstring);
$result = '';
for ($i = 0; $i < $length; $i++) {
$number[$i] = strpos($chars, $numstring{$i});
}
do {
$divide = 0;
$newlen = 0;
for ($i = 0; $i < $length; $i++) {
$divide = $divide * $frombase + $number[$i];
if ($divide >= $tobase) {
$number[$newlen++] = (int)($divide / $tobase);
$divide = $divide % $tobase;
} elseif ($newlen > 0) {
$number[$newlen++] = 0;
}
}
$length = $newlen;
$result = $tostring{$divide} . $result;
}
while ($newlen != 0);
return $result;
}
CJ Dennis
06-Apr-2006 01:03
06-Apr-2006 01:03
Really huge numbers might be truncated at both ends.
eg:
<?php
$binary="11010101001111010001110101000100011110010110110".
"001111000010001010001111001100011010110110010010011010".
"001011010000001001011111110001010101101101011010101010".
"000100011101110010110010100111110001010010111010110011".
"001111111100011001011011001110001111110000101011010010";
print(strtoupper(base_convert($binary, 2, 16)));
?>
will output:
9E8EA23CB63C0000000000000000000000000000000000000000000000000000 (64 hex digits)
when the correct result would be:
6A9E8EA23CB63C228F31AD9268B4097F156D6AA11DCB29F14BACCFF196CE3F0AD2 (66 hex digits)
Notice that as well as the result showing '0's after B63C which you would expect it is also missing the first 6A before 9E.
david dot leppek at paybytouch dot com
17-Nov-2005 07:54
17-Nov-2005 07:54
I was working on an application that needed to convert a 16 digit HEX number to BINARY. base_convert was choking when the binary number exceeded 54 characters.
$hex_value = B76ADDCE71CCC6BE;
$Sample = base_convert ($hex_value, 16, 2);
print $Sample;
//printed: 1011011101101010110111011100111001110001110011001100100000000000
Reviewing the examples here, I didnt find anything that worked for my exact need. Here is what I came up with:
function big_convert($string_in, $hex, $bin){
$temp=preg_split('//',$string_in,-1,PREG_SPLIT_DELIM_CAPTURE);
for($i=1;$i< strlen($string_in)+1; $i++) {
$results .= str_pad(base_convert($temp[$i], $hex, $bin), 4, '0', STR_PAD_LEFT);
}
return $results;
}
$hex_value = B76ADDCE71CCC6BE;
$Sample = big_convert ($hex_value, 16, 2);
print $Sample;
//printed: 1011011101101010110111011100111001110001110011001100011010111110
Hope this helps... David Leppek
rithiur at mbnet dot fi
27-Jul-2005 08:23
27-Jul-2005 08:23
Here is a simple function that works like the function provided by Mr. Fips (See below) with the exception that this can convert numbers of any size. However, note that as the division is performed manually to the number, this function is not very efficient and may not be suitable for converting strings with more than a few hundred numbers (depending on the number bases).
<?php
function custombase_convert_big ($numstring, $frombase, $tobase)
{
$from_count = strlen($frombase);
$to_count = strlen($tobase);
$length = strlen($numstring);
$result = '';
for ($i = 0; $i < $length; $i++)
{
$number[$i] = strpos($frombase, $numstring{$i});
}
do // Loop until whole number is converted
{
$divide = 0;
$newlen = 0;
for ($i = 0; $i < $length; $i++) // Perform division manually (which is why this works with big numbers)
{
$divide = $divide * $from_count + $number[$i];
if ($divide >= $to_count)
{
$number[$newlen++] = (int)($divide / $to_count);
$divide = $divide % $to_count;
}
elseif ($newlen > 0)
{
$number[$newlen++] = 0;
}
}
$length = $newlen;
$result = $tobase{$divide} . $result; // Divide is basically $numstring % $to_count (i.e. the new character)
}
while ($newlen != 0);
return $result;
}
$HEX = "0123456789ABCDEF";
$DEC = "0123456789";
print custombase_convert_big ("FFFFFF", $HEX, $DEC); // Prints "16777215"
$BIN = "01";
$ASCII = "";
for ($i = 0; $i < 256; $i++)
{
$ASCII .= chr($i);
}
$msg = "0100100101110100001000000101011101101111011100100110101101110011";
print custombase_convert_big($msg, $BIN, $ASCII); // Prints "It Works"
?>
fragmer[at]mail[dot]ru
01-May-2005 08:58
01-May-2005 08:58
Here is a much simpler and faster version of the "custombase_convert" function proposed by Mr.Fips (see below). My functions convert decimals to and from custom bases:
<?php
// Decimal > Custom
function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
}
$out = "";
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
?>
Parameters:
$num - your decimal integer
$base - base to which you wish to convert $num (leave it 0 if you are providing $index or omit if you're using default (62))
$index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "zyxwvu")
<?php
// Custom > Decimal
function any2dec( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
}
$out = 0;
$len = strlen( $num ) - 1;
for ( $t = 0; $t <= $len; $t++ ) {
$out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t );
}
return $out;
}
?>
Parameters:
$num - your custom-based number (string) (ex.: "11011101")
$base - base with which $num was encoded (leave it 0 if you are providing $index or omit if you're using default (62))
$index - if you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "abcdef")
I have optimized the functions as much as I could, I hope they'll be helpful to someone.
lindsay at bitleap dot com
18-Mar-2005 01:17
18-Mar-2005 01:17
If you need to use base_convert with numbers larger then 32 bit, the following gmp implementation of base_convert should work.
<?php
/*use gmp library to convert base. gmp will convert numbers > 32bit*/
function gmp_convert($num, $base_a, $base_b)
{
return gmp_strval ( gmp_init($num, $base_a), $base_b );
}
?>
tomhiggy at gmail dot com
16-Mar-2005 03:28
16-Mar-2005 03:28
I was looking for a function to convert the base encoding of a sha1 hash from base 16 to a custom base 32 (using characters [A-Z2-7], as used by several filesharing applications).
The function custombase_convert() posted by Fips isn't able to cope with numbers above a certain value. It returns the first few (around 10) characters correctly but each character after is a 0 value ('A' in this case).
I've solved the problem for now by requesting in groups of 10 base 16 characters and appending the result to the final base 32 value. If anyone has any better solutions, please let me know.
mike at we cant lose dot com
13-Feb-2005 09:52
13-Feb-2005 09:52
There are a few functions posted here to inverse or invert a hex color code yet none of them have actually worked for me.
This function will take a hex color code string such as '#00FF00' and return the inversed or inverted hex color code string of '#FF00FF'.
<?
function inverseColor($hex) {
if(substr($hex, 0, 1) == '#') $hex = substr($hex, 1);
$r = str_pad(dechex(255 - hexdec(substr($hex, 0, 2)) ),2,'0',STR_PAD_LEFT);
$g = str_pad(dechex(255 - hexdec(substr($hex, 2, 2)) ),2,'0',STR_PAD_LEFT);
$b = str_pad(dechex(255 - hexdec(substr($hex, 4, 2)) ),2,'0',STR_PAD_LEFT);
return strtoupper('#'.$r.$g.$b);
}
?>
simon at simonster dot com
15-Jun-2004 06:05
15-Jun-2004 06:05
Here are some functions for converting integers to and from base 256. Converting to base 64 is simple given these.
<?php
function to_base256($number, $from_base = 10) {
$binary_number = base_convert($number, $from_base, 2);
$final_string = "";
$new_length = (ceil(strlen($binary_number)/8)*8);
$binary_number = str_pad($binary_number, $new_length, "0", STR_PAD_LEFT);
for($i=($new_length-8); $i>=0; $i-=8) {
$final_string = chr(base_convert(substr($binary_number, $i, 8), 2, 10)).$final_string;
}
return $final_string;
}
function from_base256($string, $to_base = 10) {
$number = "";
for($i=0; $i<strlen($string); $i++) {
$number .= str_pad(base_convert(ord($string{$i}), 10, 2), 8, "0", STR_PAD_LEFT);
}
return base_convert($number, 2, $to_base);
}
?>
Yes, I know that this would be more efficient if it used mod instead of base_convert, but it needs to work with integers > 32 bits.
AdamJacobMuller at AdamJacobMuller dot com
28-Apr-2004 02:20
28-Apr-2004 02:20
<?php
function binarycodedstring2dec($binary) {
$len=strlen($binary);
$rows=($len/4)-1;
if (($len%4)>0) {
$pad=$len+(4-($len%4));
$binary=str_pad($binary,$pad,"0",STR_PAD_LEFT);
$len=strlen($binary);
$rows=($len/4)-1;
}
$x=0;
for ($x=0;$x<=$rows;$x++) {
$s=($x*4);
$bins=$binary[$s].$binary[$s+1].$binary[$s+2].$binary[$s+3];
$num=base_convert($bins,2,10);
if ($num>9) {
die("the string is not a proper binary coded decimal\n");
} else {
$res.=$num;
}
}
return $res;
}
?>
a binary coded decimal is converted by taking groups of four from a decimal string,
for example the binary coded decimal string
1000 = 8
10001000 does not = 136 but 88
so
binarycodedstring2dec(1000) = 8
binarycodedstring2dec(11100000111001)=3839
binarycodedstring2dec(100000111001)=839
i truly have no idea if this function will be useful to anyone, i simply failed a physics midterm because i didn't know this so i wrote this function to make sure i would never forget how to convert binary coded decimals
Matt AKA Junkie
24-Apr-2004 01:11
24-Apr-2004 01:11
You seem to not see the simplicity of using assembly-esque ideals when inverting colors...
<?
// precond: $color is a hex integer
// postcond: returns inversion
function InvertColor($color)
{
return (int) 0xffffff - $color;
}
?>
kx
25-Mar-2004 01:06
25-Mar-2004 01:06
There's an useful function custombase_convert(), which seems to be very useful, for example, to implement genealogical trees model proposed by Miguel Sofer (http://darwin.zoology.gla.ac.uk/~rpage/ MyToL/www/downloads/trees.pdf). Unfortunately, Fips' code has a bug. To make it work correctly you should replace line
while ($decVal > 0)
with this:
while ($decVal > 0 || $pos >=0)
hkh at netnords dot dk
26-Feb-2004 09:36
26-Feb-2004 09:36
An optimized version of fiftytoo InvertColor() function.
<?php
function InvertColor($hex)
{
return sprintf("%06X", $hex ^ 0xFFFFFF);
}
?>
fiftytoo at buckeyeexpress dot com
24-Dec-2003 12:57
24-Dec-2003 12:57
I needed a function to invert a hex value, which i used for setting font colors when they were on a colored background, that way i will get a contrasting color.
Im sure there are other reasons to use this, you decide!!
<?php
function InvertColor($hex) {
return sprintf("%06s",base_convert(($hex ^ 0xFFFFFF),10,16));
};
print '<td bgcolor="BB2222"><font color="'.InvertColor(0xBB2222).'">Blah</font></td>';
// Prints 44dddd as the font color, which is it's opposite on the color wheel
?>
Fips
29-Jul-2003 11:08
29-Jul-2003 11:08
I wrote a function for converting numbers not only into 0-9 and a-z:
-----
<?php
function custombase_convert($numstring, $baseFrom = "0123456789", $baseTo = "0123456789")
{
$numstring = (string) $numstring;
$baseFromLen = strlen($baseFrom);
$baseToLen = strlen($baseTo);
if ($baseFrom == "0123456789") // No analyzing needed, because $numstring is already decimal
{
$decVal = (int) $numstring;
} else {
$decVal = 0;
for ($len = (strlen($numstring) - 1); $len >= 0; $len--)
{
$char = substr($numstring, 0, 1);
$pos = strpos($baseFrom, $char);
if ($pos !== FALSE)
{
$decVal += $pos * ($len > 0 ? pow($baseFromLen, $len) : 1);
}
$numstring = substr($numstring, 1);
}
}
if ($baseTo == "0123456789") // No converting needed, because $numstring needs to be converted to decimal
{
$numstring = (string) $decVal;
} else {
$numstring = FALSE;
$nslen = 0;
$pos = 1;
while ($decVal > 0)
{
$valPerChar = pow($baseToLen, $pos);
$curChar = floor($decVal / $valPerChar);
if ($curChar >= $baseToLen)
{
$pos++;
} else {
$decVal -= ($curChar * $valPerChar);
if ($numstring === FALSE)
{
$numstring = str_repeat($baseTo{1}, $pos);
$nslen = $pos;
}
$numstring = substr($numstring, 0, ($nslen - $pos)) . $baseTo{$curChar} . substr($numstring, (($nslen - $pos) + 1));
$pos--;
}
}
if ($numstring === FALSE) $numstring = $baseTo{1};
}
return $numstring;
}
?>
------
The function arguments:
$numstring - String with number to convert. (e.g. "15" or "5F")
$baseFrom - Chars of the base the number is in. (e.g. "0123456789" for decimal or "01" for binary)
$baseTo - Chars of the base to convert the number. (e.g. "0123456789" for decimal or "01" for binary)
I wrote it for writing numbers in files, and so I convert between "0123456789" (decimal) and "\x01\x02\x03...\xFF" - uses very low disc space :-) (You could of course also add the null char (\x00), but >I< need it to seperate the numbers)
sam_at_compasspointmedia.com
07-Dec-2002 02:15
07-Dec-2002 02:15
If you haven't already figured it out from the text, the higest base you can encode in is 36 (because z is 35), try this
<?php
$compactIt = base_convert('999',10,37); //or anything higher
?>
and you'll get a warning.
gateschris at yahoo dot com
26-Mar-2001 04:57
26-Mar-2001 04:57
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);
?>
tim_converse at yahoo dot com
19-Jan-2000 03:24
19-Jan-2000 03:24
base_convert expects its string arguments to be integral, and gives nonsensical answers when given floating-point strings. For example, base_convert("1.0", 10, 2) yields "1100100". It seems that "1.0" is being interpreted as "100". (PHP4.0b3).
scott at mha dot ca
08-Sep-1999 03:48
08-Sep-1999 03:48
This would seem to be limited to 31 bits, ie. the range of values that can be converted is 0 - 2147483647 in decimal.
Also, this will convert negative values to positive values, unlike DecHex() which will return a null string for negative values.
From what I have observed (using PHP 3.0.11) the following statements will set both a and c to a null string and set b to positive 1.
<?php
$a = base_convert (2147483648, 10, 16);
$b = base_convert (-1, 10, 16);
$c = dechex (-1);
?>
