Free Online Tutorials
The PHP’s core offers some function to convert data between binary, decimal, hexadecimal and etc. But how to convert from binary to text, we offer some function to do that.
Method 1: using functions pack and base_convert
echo binToStr('1110100011101010111010001101111'); //tuto
But with the long binary data, we can’t convert
echo binToStr('0111010001110101011101000110111101110010011010010110000101101100011100110111000001101111011101000111001100101110011000110110111101101101'); //tutori`
Because of the function base_convert can’t work with the big number. The limitation is:
echo(PHP_INT_MAX);//2147483647
What’s the sulution? we can split the binary into chunks, convert each chunk to string and join them. We must change the above function:
function binToStr($input) < if (!is_string($input)) return false; $chunks = str_split($input,8); $ret = ''; foreach ($chunks as $chunk) < $temp = base_convert($chunk, 2, 16); $ret .= pack('H*',str_repeat("0", 2 - strlen($temp)) . $temp); >return $ret; >
echo binToStr('0111010001110101011101000110111101110010011010010110000101101100011100110111000001101111011101000111001100101110011000110110111101101101'); //tutorialspots.com
Method 2: use function bindec and chr to convert
function binToStr($input) < if (!is_string($input)) return false; $chunks = str_split($input,8); $ret = ''; foreach ($chunks as $chunk) < $ret .= chr(bindec($chunk)); >return $ret; >
echo binToStr('01110000011010000111000001110100011101010111010001110011'); //phptuts
An Example of Echoing Binary File Code in PHP
The JavaScript code will transfer the $_GET variables to test.php to initiate a new window for downloading, while displaying a message in the current window. Additionally, using the «split» function with a value of 1 or «.» will split the string into bytes instead of characters, especially when dealing with a multi-byte encoded string.
How to echo and filedownload together in php
As per @Mark Backer’s comment, it is not possible to both echo and download a file simultaneously for a single request. However, there are certain methods that can be used to echo a message and still download the file.
Assume that your code, as provided in the question, is present in a file named test.php. If the user requests the file containing the following code, then the scenario arises.
The JavaScript code will transfer the $_GET variables to test.php, initiate a new window for downloading, and display your message in the current window.
The Best PHP Examples, These are special codes that put characters in your string that represent typically invisible characters. Examples include newlines \n, tabs \t, and actual backslashes \\. You can also embed PHP variables in double quoted strings to have their values added to the string.
Php read file as binary
$data = fopen ($image, 'rb'); $size=filesize ($image); $contents= fread ($data, $size); fclose ($data);
File to binary php Code Example, PHP queries related to “file to binary php” php read binary file; write binary file php; php get binary file; php access to …
Convert a file to binary php
file to binary php
$data = fopen ($image, 'rb'); $size=filesize ($image); $contents= fread ($data, $size); fclose ($data);
Php save binary code to file Code Example, PHP queries related to “php save binary code to file” php read binary file; read file as binary php; php echo binary file; php convert string to binary format; copy env example to .env in laravel; Copy file …
Binary data shell echo generator
Separating concerns, Simplifying code
The function is quite complex as $add is called in every iteration of the for loop. To simplify the series of if statements, a mapping of values to add can be created instead, as demonstrated in $replacements in the sample below. Additionally, $add function seems unnecessary as the loop can easily determine the number of characters to add to $line_length and add the character to $ret .
If the characters were mapped to an array and then split into lines using array_chunk() and implode() , the conditional block at the beginning of the loop could be removed.
Initializing line length variable
The start of the string could be used to initialize the variable $ret .
Instead of using a repeated hard-coded string for the length, it could be set in a different way.
$line_length = strlen("echo -ne '");
It can just reference that string:
It goes without saying that the function’s returned value would need to be updated.
Additionally, this code can be used to reset the line length while inside the loop.
It’s possible that PHP is already handling that as a micro-optimization.
Iterating over the string
To avoid using a standard for loop when iterating over a string, an alternative option is to use an array with a foreach . Generating an array can be achieved through various methods, such as str_split() which will split a multi-byte encoded string into bytes rather than characters. Another option is to use mb_split() .
foreach(str_split($binary) as $i => $curr)
It's unnecessary to manually set $curr within the loop.
Excess continue
At the conclusion of the for loop, there is a continue statement that is unnecessary. Although it has no impact, it may cause confusion for anyone who reads the code, including yourself in the future.
Updated Code
I tried out this code on the website called tehplayground.com.
function encodeChar($char) < //could be declared as a constant $replacements = [ "\\" =>"\\\\", '\'' => '\'\\\'\'', "\n" => "\\n", "\r" => "\\r" ]; // http://www.asciitable.com/ $specialAsciiWhitelist = " !\"#$%&'()*+,-./:;?@[\\]^_`<|>~"; if (isset($replacements[$char])) < return $replacements[$char]; >if (ctype_alnum($char) || strpos($specialAsciiWhitelist, $char) !== false) < return $char; >// some binary-ish or unicode-ish data, hex-escape it.. $hex = bin2hex($char); $hex = str_split($hex, 2); return '\\x' . implode('\\x', $hex); > /** * generate command to echo (binary?) data to stdout * * @param string $binary * the (optionally binary) data to echo * @param int $max_ish_line_length * the circa-max line length for the data (PS! it's not accurate, it wraps at *circa* this length) * @return string */ function generateBinaryEcho(string $binary, int $max_ish_line_length = 50): string < $inner_max_ish_line_length = (- 2) + $max_ish_line_length; $ret = "echo -ne '"; $line_length = strlen($ret); foreach(str_split($binary) as $curr) < if ($line_length >= $inner_max_ish_line_length) < $ret .= "'\\\n'"; $line_length = 1;//strlen("'"); >$encodedChar = encodeChar($curr); $line_length += strlen($encodedChar); $ret .= $encodedChar; > return $ret . "'"; >
Php - binary data shell echo generator, \$\begingroup\$ thanks for reviewing it! several good points, and i'm glad no actual bugs were found. (this next part doesn't really matter, but the overhead of str_split() is much greater than what would be saved with the strlen() optimization, but php 7.1 added const strlen optimization anyway, strlen("'") …
bin2hex
Returns an ASCII string containing the hexadecimal representation of string . The conversion is done byte-wise with the high-nibble first.
Parameters
Return Values
Returns the hexadecimal representation of the given string.
Examples
Example #1 bin2hex() example
$hex = bin2hex ( 'Hello world!' );
var_dump ( $hex );
var_dump ( hex2bin ( $hex ));
?>
The above example will output:
string(24) "48656c6c6f20776f726c6421" string(12) "Hello world!"
See Also
User Contributed Notes 6 notes
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:
$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.
A good option for creating strings with binary data for saving (for example saving an sql statement to a file) into text files or php code is to do the following:
$field = bin2hex ( $field );
$field = chunk_split ( $field , 2 , "\\x" );
$field = "\\x" . substr ( $field , 0 ,- 2 );
?>
this will convert your field (binary or not) into hex and then convert the hex into a string which may be placed in a php file:
Convenient way of generating API keys
$apikey = bin2hex ( random_bytes ( 32 )); // generates 64 characters long string /^[0-9a-f]$/
?>
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:
would produce the following address:
%70%65%64%72%61%6d%40%72%65%64%68%69%76%65%2e%63%6f%6d
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.
Here's a function to check if a string contains any 7-bit GSM characters.
It might come useful for people working on SMS platforms.
function check_gsm ( $str )
<
$arr = array(
"0x00" , "0x01" , "0x02" , "0x03" , "0x04" , "0x05" , "0x06" , "0x07" , "0x08" , "0x09" ,
"0x0A" , "0x0B" , "0x0C" , "0x0D" , "0x0E" , "0x0F" , "0x10" , "0x11" , "0x12" , "0x13" ,
"0x14" , "0x15" , "0x16" , "0x17" , "0x18" , "0x19" , "0x1A" , "0x1B" , "0x1B0A" ,
"0x1B14" , "0x1B28" , "0x1B29" , "0x1B2F" , "0x1B3C" , "0x1B3D" , "0x1B3E" ,
"0x1B40" , "0x1B65" , "0x1C" , "0x1D" , "0x1E" , "0x1F" , "0x20" , "0x21" , "0x22" ,
"0x23" , "0x24" , "0x25" , "0x26" , "0x27" , "0x28" , "0x29" , "0x2A" , "0x2B" , "0x2C" ,
"0x2D" , "0x2E" , "0x2F" , "0x30" , "0x31" , "0x32" , "0x33" , "0x34" , "0x35" , "0x36" ,
"0x37" , "0x38" , "0x39" , "0x3A" , "0x3B" , "0x3C" , "0x3D" , "0x3E" , "0x3F" , "0x40" ,
"0x41" , "0x42" , "0x43" , "0x44" , "0x45" , "0x46" , "0x47" , "0x48" , "0x49" , "0x4A" ,
"0x4B" , "0x4C" , "0x4D" , "0x4E" , "0x4F" , "0x50" , "0x51" , "0x52" , "0x53" , "0x54" ,
"0x55" , "0x56" , "0x57" , "0x58" , "0x59" , "0x5A" , "0x5B" , "0x5C" , "0x5D" , "0x5E" ,
"0x5F" , "0x60" , "0x61" , "0x62" , "0x63" , "0x64" , "0x65" , "0x66" , "0x67" , "0x68" ,
"0x69" , "0x6A" , "0x6B" , "0x6C" , "0x6D" , "0x6E" , "0x6F" , "0x70" , "0x71" , "0x72" ,
"0x73" , "0x74" , "0x75" , "0x76" , "0x77" , "0x78" , "0x79" , "0x7A" , "0x7B" , "0x7C" ,
"0x7D" , "0x7E" , "0x7F" );
$strl = strlen ( $str );
for ( $i = 0 ; $i < $strl ; $i ++)
<
$char = '0x' . bin2hex ( substr ( $str , $i , 1 ));
$pos = in_array ( $char , $arr );
if ( $pos == 1 )
<
$j ++;
>
>