By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,812 Members | 1,288 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,812 IT Pros & Developers. It's quick & easy.

how do you convert a string to an integer array?

P: n/a

I know how to conver a string to an array of strings, but I need to convert an ascii string to an
array of integers (really unsigned chars). Eg,

$str ="ABC";

needs to convert to something like this:

$buf = array(0x41, 0x42, 0x43);

Anyone know how? I haven't been able to find a way.

David

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David Lawson <da**********@xdevnullx.com> wrote:
$str ="ABC";

needs to convert to something like this:

$buf = array(0x41, 0x42, 0x43);

Anyone know how? I haven't been able to find a way.


See the string functions: http://www.php.net/manual/en/function.ord.php
and convert those values to hex, of use
http://www.php.net/manual/en/function.bin2hex.php and some (regexp)
magic.

Jul 17 '05 #2

P: n/a
On Wed, 30 Mar 2005 16:26:13 -0500, David Lawson wrote:
I know how to conver a string to an array of strings,
but I need to convert an ascii string to an
array of integers (really unsigned chars).


$s = 'ABC';
$a = array_map('ord', preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY));

Or for PHP5:

$a = array_map('ord', str_split($s));

Gives:

$a => Array
(
[0] => 65
[1] => 66
[2] => 67
)

--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
On Wed, 30 Mar 2005 23:48:53 +0200, Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
On Wed, 30 Mar 2005 16:26:13 -0500, David Lawson wrote:
I know how to conver a string to an array of strings,
but I need to convert an ascii string to an
array of integers (really unsigned chars).


$s = 'ABC';
$a = array_map('ord', preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY));

Or for PHP5:

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

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.

David

Jul 17 '05 #4

P: n/a
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
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.