How can I convert array of bytes to a string in PHP?
If by array of bytes you mean:
$bytes = array(255, 0, 55, 42, 17, );
array_map()
Then it's as simple as:
$string = implode(array_map("chr", $bytes));
foreach()
Which is the compact version of:
$string = "";
foreach ($bytes as $chr) {
$string .= chr($chr);
}
// Might be a bit speedier due to not constructing a temporary array.
pack()
But the most advisable alternative could be to use pack("C*", [$array...])
, even though it requires a funky array workaround in PHP to pass the integer list:
$str = call_user_func_array("pack", array_merge(array("C*"), $bytes)));
That construct is also more useful if you might need to switch from bytes C* (for ASCII strings) to words S* (for UCS2) or even have a list of 32bit integers L* (e.g. a UCS4 Unicode string).
Convert byte array to string in PHP
Please try this function to convert a GUID into a binary string:
function guidToBytes($guid) {
$guid_byte_order = [3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15];
$guid = preg_replace("/[^a-zA-Z0-9]+/", "", $guid);
$result = [];
foreach ($guid_byte_order as $offset) {
$result[] = hexdec(substr($guid, 2 * $offset, 2));
}
return $result;
}
Array of bytes to UTF-8 string in PHP?
A string is nothing more than an array of bytes. So a UTF-8 string is the very same as an array of bytes, except that in addition you know what the array of bytes represent.
So your input array of bytes needs one more additional information as well: the character set (character encoding). If you know the input character set, you can convert the array of bytes to another array of bytes representing an UTF-8 string.
The PHP method for doing that is called mb_convert_encoding()
.
PHP itself does not know of character sets (character encodings). So a string really is nothing more than an array of bytes. The application has to know how to handle that.
So if you have an array of bytes and want to turn that into a PHP string in order to convert the character set using mb_convert_encoding()
, try the following:
$input = array(0x53, 0x68, 0x69);
$output = '';
for ($i = 0, $j = count($input); $i < $j; ++$i) {
$output .= chr($input[$i]);
}
$output_utf8 = mb_convert_encoding($output, 'utf-8', 'enter input encoding here');
(Instead of the single example above, have a look at more examples at https://stackoverflow.com/a/5473057/530502.)
$output_utf8
then will be a PHP string of the input array of bytes converted to UTF-8.
PHP Convert byte array as string into pdf
A quick test suggests the string is encoded as Base16 (hexadecimal) (an odd choice).
PHP has a handy function available in hex2bin()
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="service.pdf"');
echo hex2bin($byte_string);
exit;
String to byte array in php
@Sparr is right, but I guess you expected byte array like byte[]
in C#. It's the same solution as Sparr did but instead of HEX you expected int
presentation (range from 0 to 255) of each char
. You can do as follows:
$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
var_dump($byte_array); // $byte_array should be int[] which can be converted
// to byte[] in C# since values are range of 0 - 255
By using var_dump
you can see that elements are int
(not string
).
array(44) { [1]=> int(84) [2]=> int(104) [3]=> int(101) [4]=> int(32)
[5]=> int(113) [6]=> int(117) [7]=> int(105) [8]=> int(99) [9]=> int(107)
[10]=> int(32) [11]=> int(102) [12]=> int(111) [13]=> int(120) [14]=> int(32)
[15]=> int(106) [16]=> int(117) [17]=> int(109) [18]=> int(112) [19]=> int(101)
[20]=> int(100) [21]=> int(32) [22]=> int(111) [23]=> int(118) [24]=> int(101)
[25]=> int(114) [26]=> int(32) [27]=> int(116) [28]=> int(104) [29]=> int(101)
[30]=> int(32) [31]=> int(108) [32]=> int(97) [33]=> int(122) [34]=> int(121)
[35]=> int(32) [36]=> int(98) [37]=> int(114) [38]=> int(111) [39]=> int(119)
[40]=> int(110) [41]=> int(32) [42]=> int(100) [43]=> int(111) [44]=> int(103) }
Be careful: the output array is of 1-based index (as it was pointed out in the comment)
Convert an array into string or object
I managed to reproduce your "Array to string conversion" error when using the implode
command by running the following line of code:
implode(";", [[]]); // PHP Notice: Array to string conversion in php shell code on line 1
For converting a nested array into a string I found that a foreach
loop worked:
$nestedArray = ['outerKeyOne' => ['innerKeyOne' => 'valueOne'], 'outerKeyTwo' => ['innerKeyTwo' => 'valueTwo']];
$arrayOfStrings = [];
foreach ($nestedArray as $key => $value) {
$arrayOfStrings[] = implode(",", $value);
}
implode(";", $arrayOfStrings); // string(17) "valueOne;valueTwo"
The second error associated with the line $val = (object) $list;
is from trying to embed an object into the $sql
string. It seems like an object is not what you want here, unless it is an object that has a __toString()
method implemented.
I hope this is of some help. Using var_dump
or something similar would provide more debug output to better diagnose the problems along with the above error messages. That's how I came up with the above code.
How do I get the byte values of a string in PHP?
Use the ord function
http://ca.php.net/ord
eg.
<?php
$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";
for($i = 0; $i < strlen($var); $i++)
{
echo ord($var[$i])."<br/>";
}
?>
Related Topics
How to Http Post Special Chars in Swift
PHP Session-Based Flash Message
Strange Echo, Print Behaviour in PHP
Php: Session Lost on Subdomain
How to Parse Xml Containing Custom Namespaces Using Simplexml
How to Avoid "Using Temporary" in Many-To-Many Queries
Display Only 3 Foreach Result Per Row
Modify an Existing PHP Function to Return a String
PHP Prepared Statement Problems
Disable Template Caching for Development in Opencart 3
Getting Warning "Header May Not Contain More Than a Single Header, New Line Detected"
PHP Removing HTML Tags from String
Process Very Big CSV File Without Timeout and Memory Error
Get All Records from MySQL Database That Are Within Google Maps .Getbounds
Wamp - Remove Localhost from Project Url
Is There Java Hashmap Equivalent in PHP
PHP Notice: Undefined Offset: 1 with Array When Reading Data