User Contributed Notes bin2hex |
 |
josh@superfork.com
23-Jun-1999 12: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 07:50 |
|
function hex2bin($data) {
$len =
strlen($data);
return pack("H" . $len,
$data);
}
|
|
lulu@ctyd.com.mx
15-Dec-1999 03:12 |
|
Another one:
function hex2bin($hex_data){
return
(DecBin(HexDec($hex_data));
}
|
|
jon@tgpsolutions.com
17-Nov-2000 02:54 |
|
PHP has a built-in function: base_convert()
$binary =
base_convert($hexadecimal, 16, 2);
|
|
pedram@redhive.com
01-Feb-2001 12: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%2Ffr.php.net%2Fmanual%2Fde%2F%26quot%3B%3Ca&y=1999
href="mailto:<?=hex_encode("pedram@redhive.com"">mailto:<?=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 07: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
26-Aug-2001 03: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
16-Sep-2001 04: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 06: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;
};
|
|
php@(what spam?)@vaejas.com
05-Aug-2002 12:01 |
|
I have found the above quoted-printable encoding function to
be extremely useful, and thank maxg for making a great function that is
not already included in PHP. (Pity it's hidden all the way over here
when it could be found more easily in quoted_printable_decode()
)
However, there is one missing character in the code that needs to
be included:
Right after the ?Q? there needs to be an extra equal
sign in order for the very first character to be translated by the e-mail
software.
$string = "=?$charset?Q?=" . $encoded .
"?="; is correct.
I don't yet have enough experience
to speak for other caveats or improvements, but your mileage may vary
depending on the e-mail server.
- Luke
|
|
matt@zevi.net
21-Aug-2002 09:27 |
|
Hopefully this helps someone...
It just displays an html
representation of hex data, much like a hex viewer
would.
Matt
---- PS - you may have to indent it yourself -
the formatting of the notes doesn't seem to be quite
right. ----
function hexview($data){ $bytePosition =
$columnCount = $lineCount = 0; $columns = 8; $dataLength =
strlen($data); $return = array(); $return[] = '<table
border="1" cellspacing="0"
cellpadding="2">'; for($n = 0; $n < $dataLength;
$n++){ $lines[$lineCount][$columnCount++] = substr($data, $n,
1); if($columnCount == $columns){ $lineCount++;
$columnCount = 0; } } foreach($lines as $line){
$return[] = '<tr><td align="right">'.$bytePosition.':
</td>'; for($n = 0; $n < $columns; $n++){
$return[] = '<td>'.strtoupper(bin2hex($line[$n])).'</td>';
} $return[] = '<td>
</td>'; for($n = 0; $n < $columns; $n++){
$return[] = '<td>'.(htmlentities($line[$n]) ? htmlentities($line[$n])
: ' ').'</td>'; } $return[] =
'</tr>'; $bytePosition = $bytePosition + $columns; }
$return[] = '</table>'; return implode('', $return); }
|
|
 |