On Thu, 31 Mar 2005 10:18:39 -0500, David Lawson <da**********@xdevnullx.com>
wrote:
On Wed, 30 Mar 2005 23:48:53 +0200, Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
$a = array_map('ord', str_split($s));
Gives:
$a => Array
(
[0] => 65
[1] => 66
[2] => 67
)
thanks,
$a = array_map('ord', str_split($s));
did what I needed.
Now I have a binary (integer) array and need to write it to a file.
Well, you have an array of integers. Not "unsigned chars" as you wanted
..I've been experimenting with
pack, fwrite, and file_put_contents but can't get any of these to work.
Do I need to convert the (modified) integer array back to a binary string first?
I've tried pack():
$bstr = pack("C*", $buf2); // convert buf2 to a binary string
file_put_contents($this->tmpname, $bstr);
This only writes 1 byte to a file. The count of $buf2 is 256.
Er, let's back up a step here. Unlesss I've missed some subtlety here, you
said:
$str ="ABC";
needs to convert to something like this:
$buf = array(0x41, 0x42, 0x43);
And you want to write that to a file. Well, just write "ABC". It's the same
thing. It sounds like you've gone full-circle from having a string, turning it
into an array of integer codes that each character is represented by, to
writing back those integers as bytes - when exactly the same effect is given by
just writing the string in the first place..?
In fact by going via ord() you're entering the world of character set
encodings; ord() only claims to handle ASCII which is 0-127. Although you did
mention ASCII originally so you're probably safe.
--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool