How to Get the Byte Values of a String in PHP

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/>";
}
?>

PHP - Create a valid byte string

Use http://php.net/manual/en/function.hex2bin.php

Like that:

$codes = ["02", "66", "6c", "6a", "3a", "03"];
$byteString1 = hex2bin(implode('', $codes));

How can I get the single bytes from a multibyte PHP string variable in a binary-safe way?

you can get a bytearray by unpacking the utf8_encoded string $a:

$a = utf8_encode('Fön');
$b = unpack('C*', $a);
var_dump($b);

used format C* for "unsigned char"

References

  • String to byte array in php
  • http://www.php.net/manual/en/function.unpack.php
  • http://www.php.net/manual/en/function.pack.php

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)

PHP Read Byte Range from String or File

You should be able to do this with fseek and fread.

<?php
$byteOffset = 1024;
$readLength = 256;
$fileHandle = fopen('myfile', 'r');
fseek($fileHandle, $byteOffset);
$bytes = fread($fileHandle, $readLength);

String to byte/binary arrays in PHP

I think you are asking for the equivalent to the Perl pack/unpack functions. If that is the case, I suggest you look at the PHP pack/unpack functions:

  • Unpack
  • Pack

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).



Related Topics



Leave a reply



Submit