Link to home
Start Free TrialLog in
Avatar of j-tech
j-tech

asked on

How do I convert an int to byte array in PHP?

I need to convert an integer value to a 2 byte array and then to a 2 character string to write to a text file.  I obtain the value from the file using:

$fhandle = fopen($this->FilePath, 'r');
$myVal  = unpack('S',fread($fhandle, 2));
echo 'My Val = '.$myVal[1]; //let's say that $myVal = 1234

How do I convert the value back to the original format from an integer?

Thank you,

j
ASKER CERTIFIED SOLUTION
Avatar of Mathias
Mathias
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of j-tech
j-tech

ASKER

Thanks TDS, that worked perfectly.  Also helped with converting an integer to a single byte array to single character string.  Appreciate it!