note 58844 added to function.base-convert

From: Date: Thu, 17 Nov 2005 15:54:18 +0000
Subject: note 58844 added to function.base-convert
Groups: php.notes 
Request: Send a blank email to php-notes+get-98710@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.base-convert.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58844 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58844&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58844&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98710) next »