note 58844 deleted from function.base-convert by danbrown

From: Date: Sat, 02 Jul 2011 15:40:13 +0000
Subject: note 58844 deleted from function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181169@lists.php.net to get a copy of this message
Note Submitter: david dot leppek at paybytouch dot com ---- 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 didn’t 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

« previous php.notes (#181169) next »