Jump to content

How to do this in PHP?


awatson

Recommended Posts

I'm just starting to learn PHP and need to convert a section of code from one of my standalone Windows applications to the equivalent functionality in PHP.

 

In my standalone app, I allocate a memory buffer (about 64 bytes) then save and manipulate data in that buffer. For example, I might save a Long integer (4 bytes) to one location and a Short integer (2 bytes) to another location. Then read the buffer contents later byte by byte.

 

But, I don't see any equivalent to allocating a memory buffer in PHP?

 

I thought about using a string to hold the data, but can't figure out how to convert a Long integer into a four-byte string, or a short integer into a two byte string?  I also need to ensure the integer is saved in the correct order (big-endian or little-endian, as needed) regardless of which server it is run on.

 

Any tips would be appreciated.

 

Thanks,

 

Anthony

Link to comment
Share on other sites

My original Windows app is actually written in PowerBASIC, but no matter, I think I figured out how to use a PHP string for the task. The pack() command lets me create the two-byte and four-byte strings as needed with the proper format (little-endian). Simple joining of the individual strings works fine.

 

On to the next step in the code... :)

 

Thanks,

 

Anthony

Link to comment
Share on other sites

/sigh..

You C programmers! If I read any php with a null terminated character array instead of a string, I'm coming for you!

Oh PowerBASIC? nevermind :D!

 

PHP can cast pretty much anything into anything automatically, even if it doesn't make sense (array->string even works... even if it's just a string filled with the word array). You don't need to worry about memory management or casting for 99% of tasks in PHP... not that it's always a good thing.

 

$str1 = "234alfknel#%FE";
$str2 = "6321alnl3#^";
echo $str1 % $str2;

^php will even try do this without even considering that it makes no friggen sense.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.