note 68132 added to function.bin2hex

From: Date: Fri, 14 Jul 2006 13:12:24 +0000
Subject: note 68132 added to function.bin2hex
Groups: php.notes 
Request: Send a blank email to php-notes+get-114779@lists.php.net to get a copy of this message
Since PHP only has a way to convert a real binary string into hex, couldn't I find a function that can convert binary information written in ascii (like 0110). This function will convert that <? function asciibin2hex($str) { $str = str_replace(" ", "", $str); //delete some probably spaces //Binary to HEX list $binary['0000'] = "0"; $binary['0001'] = "1"; $binary['0010'] = "2"; $binary['0011'] = "3"; $binary['0100'] = "4"; $binary['0101'] = "5"; $binary['0110'] = "6"; $binary['0111'] = "7"; $binary['1000'] = "8"; $binary['1001'] = "9"; $binary['1010'] = "a"; $binary['1011'] = "b"; $binary['1100'] = "c"; $binary['1101'] = "d"; $binary['1110'] = "e"; $binary['1111'] = "f"; //make sets of 4 for( ; ; ) { $calc = strlen($str) / 4; if(is_numeric($calc)&&(intval($calc)==floatval($calc))) { break; } else { $str .= 0; } } //translate binary to hex for($i = 0 ; $i < strlen($str) ; $i = $i + 4) { $set = substr($str, $i, 4); $inhex .= $binary[$set]; } return $inhex; } //Examples: echo asciibin2hex("101101011100"); //prints: b5c echo asciibin2hex("1011 1000 1111 1100") //prints: b8fc echo asciibin2hex("1000 1011 1101 001") //prints: 8bd2 ?> I hope I didn't make it too hard for myself writing this all, but hé, it works :) ---- Server IP: 83.137.20.120 Probable Submitter: 83.160.39.21 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.bin2hex.php Edit -- http://master.php.net/note/edit/68132 Del: integrated -- http://master.php.net/note/delete/68132/integrated Del: useless -- http://master.php.net/note/delete/68132/useless Del: bad code -- http://master.php.net/note/delete/68132/bad+code Del: spam -- http://master.php.net/note/delete/68132/spam Del: non-english -- http://master.php.net/note/delete/68132/non-english Del: in docs -- http://master.php.net/note/delete/68132/in+docs Del: other reasons-- http://master.php.net/note/delete/68132 Reject -- http://master.php.net/note/reject/68132 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114779) next »