★ wanayoo — archive 1999 http://www.php.net/manual/ro/function.bin2hex.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | reporting bugs | links 
search for in the  
previousaddslasheschopnext
Last updated: Thu, 04 Apr 2002
view this page in Plain HTML | English | Brazilian Portuguese | Czech | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Russian | Spanish | Turkish

bin2hex

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

bin2hex --  Convert binary data into hexadecimal representation

Description

string bin2hex ( string str)

Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise with the high-nibble first.

See also pack() and unpack().

User Contributed Notes
bin2hex
add a note about notes
josh@superfork.com
22-Jun-1999 05:56

I wrote a short function to convert the hex string back to binary.


function hex2bin($data) {

    $len = strlen($data);
    for($i=0;$i<$len;$i+=2) {
        $newdata .=  pack("C",hexdec(substr($data,$i,2)));
    }
    return $newdata;
}

carl@five-ten-sg.com
11-Aug-1999 12:50

function hex2bin($data) {
$len = strlen($data);
return pack("H" . $len, $data);
}


lulu@ctyd.com.mx
14-Dec-1999 08:12

Another one:
function hex2bin($hex_data){
return (DecBin(HexDec($hex_data));
}

dimitry@career.ru
17-Jan-2000 06:55

echo bin2hex("abc");
Output is: 616263

jon@tgpsolutions.com
16-Nov-2000 07:54

PHP has a built-in function: base_convert()

$binary = base_convert($hexadecimal, 16, 2);

pedram@redhive.com
31-Jan-2001 05:21

In an attempt to dodge spam bots I've seen people (including myself) hex
encode their email addresses in "mailto" tags. This is the small
chunk of code I wrote to automate the process:

function hex_encode ($email_address)    {
        $encoded = bin2hex("$email_address");
        $encoded = chunk_split($encoded, 2, '%');
        $encoded = '%' . substr($encoded, 0, strlen($encoded) - 1);
        return $encoded;    
}

so for example:
<a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2Fro%2F%26quot%3B%3Ca&y=1999
href="mailto:&lt;?=hex_encode("pedram@redhive.com"">mailto:&lt;?=hex_encode("pedram@redhive.com")?>">email
me</a>

would produce the following address:
%70%65%64%72%61%6d%40%72%65%64%68%69%76%65%2e%63%6f%6d

-pedram

gherson at snet dot net
15-Mar-2001 12:56

As is, the 
function  hex2bin($hex_data){
return (DecBin(HexDec($hex_data)));
}
suggested above will return actual ones and zeroes as opposed to a binary
string interpreted as ascii. E.g., "11000010110001001100011"
gets returned instead of "abc".

tightcode@hotmail.com
25-Aug-2001 08:05

I was just browsing the above and with a little modification,
came up with the following which I believe to be more flexible:

function bin2hex($data) {
    $corrected =
ereg_replace("[^0-9a-fA-F]","",$data);
    return pack("H".strlen($corrected),$corrected);
}

This will make sure that whatever you pass, even if it is padded
at the extremeties or between pairs, should return the desired data.

mail@philipp-louis.de
15-Sep-2001 09:12

If for some reason you want to prevent your HTML output (e.g. JavaScripts)
from being stolen by simple code thiefs or parsed by dumb bots, it might
be a good idea to encrypt it using this technique:

<?php

$output = "

Blah blah blubb.

"; $output .= "

Blubb blubb blah.

"; /* Use something like this to store the output in a string instead of echo-ing it directly to the browser. */ for($count = 0; $count < strlen($output); $count++) { $escaped .= "%" . bin2hex($output[$count]); } $code = "<HTML><HEAD><SCRIPT LANGUAGE=\"javascript\">\n<!--\ndocument.write(unescape(\"$escaped\"));\n//-->\n</SCRIPT></HEAD></HTML>"; echo $code; ?> Anyone using the browser's "view source" function will see the following: <HTML><HEAD><SCRIPT LANGUAGE="javascript"> <!-- document.write(unescape("%3c%50%3e%42%6c%61%68%20%62%6c%61%68%20%62%6c%75%62%62%2e%3c%2f%50%3e%3c%50%3e%42%6c%75%62%62%20%62%6c%75%62%62%20%62%6c%61%68%2e%3c%2f%50%3e")); //--> </SCRIPT></HEAD></HTML> Of course this won't help against more serious efforts to steal your code, but it will irritate the spam and copy'n'paste folks.

maxg@hot.ee
14-Dec-2001 11:47

I wrote the function that is useful for sending emails for example in
cyrillic encoding (wondows-1251, koi8-r and so on).
It converts every symbol in the string into hexadecimal presentation with
"=" symbol as delimiter.

For example, the string "Регистрационный код для" will look like
that:
=d0=e5=e3=e8=f1=f2=f0=e0=f6=e8=ee=ed=ed=fb=e9=20=ea=ee=e4=20=e4=eb=ff

I use it for encoding email subject, but it could be modified for anything
else.

function hex4email ($string,$charset)
{
	$string=bin2hex ($string);
	$encoded = chunk_split($string, 2, '='); 
	$encoded=preg_replace ("/=$/","",$encoded);
	$string="=?$charset?Q?".$encoded."?=";
	
return $string;
};

add a note about notes
previousaddslasheschopnext
Last updated: Thu, 04 Apr 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: Synacor
Last updated: Mon Apr 15 03:30:26 2002 EDT