Jump to content

awatson

New Members
  • Posts

    3
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

awatson's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I'm working on my first PHP script, and I need to be able test the bits in a variable as well as set or reset them when needed. I've tracked down a few bit flag examples, but none of them worked for me, so I've tried to tackle it on my own. I wrote this "simple" function to get the state of a bit: function get_bit($value, $bit) { $mask=1 << $bit; $x=($value & $mask); if ($x==0) { $result=0; } else { $result=1; } echo bin2hex($value)." ".$mask." ".$x."<br />"; return $result; } $value is the incoming value (between 0 and 255) $bit is the number of the bit (0-7) The echo line is just for my testing, but it "should" show the hex value of the variable, the appropriate mask corresponding to the bit (1,2,4,8,16,32,etc.), and a variable ($x) that is either zero or matches the calculated mask. Unfortunately, while the hex value and mask appear to be calculated correctly, the $x variable is always coming out as zero. I'm a PHP newbie. What am I doing wrong? Ideally, the function should return either 0 or 1 depending on whether the selected bit is set or cleared. I also tried a simple shift approach like $result=($value >> $bit) & 1, but that didn't work either. I'm obviously overlooking something. Thanks, Anthony
  2. 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
  3. 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
×
×
  • 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.