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
https://forums.phpfreaks.com/topic/124984-how-to-do-this-in-php/
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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.