Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

bin2hex

(PHP 3 >= 3.0.9, PHP 4, PHP 5)

bin2hex --  Преобразует бинарные данные в шестнадцатиричное представление

Описание

string bin2hex ( string str )

Возвращает строку, содержащую шестнадцатиричное представление аргумента str. Преобразование производится побайтно.

См. также описание функций pack() и unpack().



chop> <addslashes
Last updated: Fri, 26 Jan 2007
 
add a note add a note User Contributed Notes
bin2hex
tehjosh at gamingg dot net
11-Aug-2007 11:35
This function is for converting binary data into a hexadecimal string representation.  This function is not for converting strings representing binary digits into hexadecimal.  If you want that functionality, you can simply do this:

<?php
$binary
= "11111001";
$hex = dechex(bindec($binary));
echo
$hex;
?>

This would output "f9".  Just remember that there is a very big difference between binary data and a string representation of binary.
ratfactor at gmail dot com
23-Nov-2006 12:13
I also wished to have the ability to translate a string containing a binary number ('1101') to a string containing a hexadecimal number ('d').  Here is my function:

function strbin2hex($bin){
  $last = strlen($bin)-1;
  for($i=0; $i<=$last; $i++){ $x += $bin[$last-$i] * pow(2,$i); }
  return dechex($x);
}

Example:
strbin2hex('1101');   // returns 'd'

I also added some optional features to my function (zero padding and upper case hex letters):

function strbin2hex($bin, $pad=false, $upper=false){
  $last = strlen($bin)-1;
  for($i=0; $i<=$last; $i++){ $x += $bin[$last-$i] * pow(2,$i); }
  $x = dechex($x);
  if($pad){ while(strlen($x) < intval(strlen($bin))/4){ $x = "0$x"; } }
  if($upper){ $x = strtoupper($x); }
  return $x;
}

Examples:
strbin2hex('11110101', true, true);   // returns 'F5'
strbin2hex('00001011', true, true);   // returns '0B'
nilzie at gmail dot com
14-Jul-2006 06:12
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
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571