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)
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 to convert string to ascii byte array in php
I know this is an old post now but for anyone else that comes across this in the future like me:
In PHP, strings are already byte arrays. So to put together the auth header for ProPay, see the following example:
$certStr = 'MyCertStr';
$termId = 'MyTermId';
$base64 = base64_encode($certStr.':'.$termId);
$authorization = 'Basic '.$base64;
//Output: "Basic TXlDZXJ0U3RyOk15VGVybUlk"
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 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 string as byte array, hashed then base64 encoded
Yes, strings in PHP are already byte arrays. And unless your $string
contains any non-ASCII characters, it's also already valid UTF-8 (UTF-8 is a superset of ASCII); so you can skip the "encode as UTF-8" step.
Likely the algorithm is expecting the output of the hash to be binary, which you're then supposed to convert to base 64. By default hash
returns hex values, not binary. For that you need to set its 3rd parameter. In summary:
$unique = uniqid();
$string = $unique . ':' . $this->apiKey;
$hash = hash('sha256', $string, true);
$base64 = base64_encode($hash);
Of course, what you're supposed to do with $unique
I don't know. Likely you're supposed to send that value together with the request as well, otherwise there's no way the server can validate the hash.
Base64 of byte array in Java and it's equivalent in PHP
PHP will already treat $string
as a byte string, so you don't need to unpack/implode it.
If you do this:
$string = 'ec65450a-5:5217e';
$bytes = unpack('C*', $string);
echo implode('', $bytes);
You get this:
1019954535253489745535853504955101
Which is a mushed together list of integer base 10 ASCII values of each character, and is almost certainly not what you want. Just encode the string directly:
echo base64_encode($string);
Result:
ZWM2NTQ1MGEtNTo1MjE3ZQ==
Also, you'll want to change your password now that you've posted it here. :)
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
Using Jquery Ajax to Retrieve Data from MySQL
Isset() and Empty() - What to Use
How to Measure the Speed of Code Written in PHP
Getting Dom Elements by Classname
Streaming a Large File Using PHP
Send File Attachment from Form Using PHPmailer and PHP
How to Set Order by Params Using Prepared Pdo Statement
"Invalid Parameter Number: Parameter Was Not Defined" Inserting Data
Get Table Column Names in MySQL
Show Values from a MySQL Database Table Inside a HTML Table on a Webpage
Performance of Foreach, Array_Map With Lambda and Array_Map With Static Function
PHP: Regex to Ignore Escaped Quotes Within Quotes