note 90408 added to function.bin2hex

From: Date: Tue, 21 Apr 2009 06:01:15 +0000
Subject: note 90408 added to function.bin2hex
Groups: php.notes 
Request: Send a blank email to php-notes+get-153532@lists.php.net to get a copy of this message
I want clear this up for everyone using functions like the one below this... <?php function bin2hex($str) { $hex = ""; $i = 0; do { $hex .= dechex(ord($str{$i})); $i++; } while ($i < strlen($str)); return $hex; } // Look what happens when ord($str{$i}) is 0...15 // you get a single digit hexadecimal value 0...F // bin2hex($str) could return something like 4a3, // decimals(74, 3), whatever the binary value is of those. function hex2bin($str) { $bin = ""; $i = 0; do { $bin .= chr(hexdec($str{$i}.$str{($i + 1)})); $i += 2; } while ($i < strlen($str)); return $bin; } // hex2bin("4a3") just broke. Now what? // Using sprintf() to get it right. function bin2hex($str) { $hex = ""; $i = 0; do { $hex .= sprintf("%02x", ord($str{$i})); $i++; } while ($i < strlen($str)); return $hex; } // now using whatever the binary value of decimals(74, 3) // and this bin2hex() you get a hexadecimal value you can // then run the hex2bin function on. 4a03 instead of 4a3. ?> ---- Server IP: 69.147.83.197 Probable Submitter: 71.211.194.247 ---- Manual Page -- http://www.php.net/manual/en/function.bin2hex.php Edit -- https://master.php.net/note/edit/90408 Del: integrated -- https://master.php.net/note/delete/90408/integrated Del: useless -- https://master.php.net/note/delete/90408/useless Del: bad code -- https://master.php.net/note/delete/90408/bad+code Del: spam -- https://master.php.net/note/delete/90408/spam Del: non-english -- https://master.php.net/note/delete/90408/non-english Del: in docs -- https://master.php.net/note/delete/90408/in+docs Del: other reasons-- https://master.php.net/note/delete/90408 Reject -- https://master.php.net/note/reject/90408 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153532) next »