You are here: Re: arrays -- can they be words, not bytes? « PHP Programming Language « IT news, forums, messages
Re: arrays -- can they be words, not bytes?

Posted by Bint on 10/17/07 21:33

Well, let me ask you. I've got the code working now, using the method I
described above. But just for my edification, how do I know if I am working
with strings or arrays?

The thing begins as a POST incoming variable, say $image.
Then if I start accessing elements of it, ie $image[0], $image[1], etc, then
am I accessing string elements?
And if I make a *new* entity, say $output, and start saying things like
$output[0] = ord($image[0]) + (ord($image[1])<<8), then am I creating a
string there, or an array?

Thanks
B



"Lars Eighner" <usenet@larseighner.com> wrote in message
news:slrnfhcucm.157i.usenet@debranded.larseighner.com...
> In our last episode,
> <13hcrsv3c5a2584@corp.supernews.com>,
> the lovely and talented Bint
> broadcast on comp.lang.php:
>
>> Yeah, I guess I'm trying to do something low-level with this high-level
>> lanugage. I'm new to PHP so I'm trying to figure out how these fancy
>> keyword-based arrays work with old-school byte arrays.
>
>> I'm sending images wirelessly to a php script. The image, originally a
>> grid
>> of pixels, each 16-bits deep, is run-length-encoded into a smaller C
>> array
>> of unsigned shorts (16 bit words). That C array is base64 encoded into
>> an
>> ASCII string so that I can send it via HTTP POST command to a PHP script.
>
>> The PHP script sees my base64 encoded array as a variable, which I can
>> easily base64 decode into a php "array". But it is proving trickier to
>> access my pixel values, because now the array is not a C array, but a PHP
>> one. If I look at the value of array[0], then I don't get the number
>> that
>> was in array[0] before I sent it.
>
>> I can work around it, by accessing each byte of the PHP array:
>
>> $myoriginalarray[0] = ord($phparray[0]) + ord($phparray[1]) << 8;
>
>> But that is complicated and I just thought there might be some simpler
>> way
>> of telling PHP "hey, I have an array of unsigned shorts here".
>> Maybe not.
>
> Treat them as a string, use the base 64 decode function which takes a
> string
> argument and returns a string result. In many respects strings are a kind
> of array, but they are not type array in PHP. Conveniently, strings are
> composed of characters, and characters bear more than a passing
> resemblance
> to 8-bit unsigned integers. Take them two at time if you have to
> manipulate
> them at the 16-bit level. See chapter 11 (Types) in the manual for hints
> as
> to how to work with strings. Don't use curly brackets, they will be
> deprecated in PHP 6, or so the scuttlebutt goes.
>
> Do not be confused by PHP array type. Although strings are kind of like
> arrays with integer indices, there are significant differences. You
> should
> be learning to use strings from the documentation on type string, and the
> documentation on type array is likely to mislead you.
>
>
> --
> Lars Eighner <http://larseighner.com/>
> <http://myspace.com/larseighner>
> Countdown: 460 days to go.
> What do you do when you're debranded?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация