tags:

views:

3034

answers:

4

How do I convert a string to a binary array in php?

A: 

To be honest, I'm unsure what you mean by binary array (GIS didnt clarify either). If you mean a c-style array with one character-per-element, then you want the str_split built-in.

EvanK
A: 

There is no such thing as a binary array in php. All functions requiring byte streams operate on strings. What is it exactly that you want to do?

soulmerge
+1  A: 

If you're trying to access a specific part of a string you can treat it like an array as-is.

$foo = 'bar';
echo $foo[0];

output: b

McAden
For this kind of string access, I believe curly brace notation is preferable (otherwise you risk confusing the hell out of anyone else maintaining your code). For example: $foo{0}
EvanK
Unless I'm mistaken, Curly brace notation for this is deprecated in PHP 6
McAden
Ah, here it is: http://us.php.net/language.types.stringThe "Note" under the heading - "String access and modification by character"
McAden
+1  A: 

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

earino
This is exactly what I was looking for.
Jeff Winkworth