?
Solved

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

Posted on 2010-08-25
2
Medium Priority
?
1,565 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:j-tech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
Mathias earned 2000 total points
ID: 33528511
Take a look at this:

Output:
----------
0x1234
LB: 0x34
HB: 0x12
0x1234
LB: 0x34
HB: 0x12
0x12345678LW: 0x5678
HW: 0x1234
0x12345678LW: 0x5678
HW: 0x1234
Start: 0x1234
Array: 0x34, 0x12,
Array: 52, 18
String: 4
Hex output
34 12                                           | 4.
----------
<?php
// Function found at: http://www.theserverpages.com/scripts/
// Please leave this comment intact when using this code!
function hexoutput($data, $width=16)
{
	$st=0;
	$txt="";
	$out="";
	for ($i=0; $i<strlen($data); $i++)
	{
		$n=ord(substr($data,$i,1));
		$c=str_pad(dechex($n),2,"0",STR_PAD_LEFT);
		$out.=$c." ";
		if ($n==32 || $n>32 && $n<127 || $n>160)
		{
			$txt.=substr($data,$i,1);
		}
		else
		{
			$txt.=".";
		}
		if ($i % $width == $width-1)
		{
			$out.="| ".$txt."\n";
			$txt="";
		}
	}
	$out.=str_repeat("   ",$width-($i % $width));
	$out.="| ".$txt;
	$out.="\n";
	return $out;
}

function LO_BYTE($x)
{
	return ($x & 0x00FF);
}
function HI_BYTE($x)
{
	return (($x & 0xFF00) >> 8);
}
function LO_WORD($x)
{
	return ($x & 0xFFFF);
}
function HI_WORD($x)
{
	return (($x >> 16) & 0xFFFF);
}
function MAKE_WORD($a, $b)
{
	return (($b << 8) + $a);
}
function MAKE_LONG($a, $b)
{
	return ($a | ($b << 16));
}

echo '<pre>';

$test = 0x1234;
echo sprintf("0x%04X\n", $test);

echo sprintf("LB: 0x%02X\n", LO_BYTE($test));
echo sprintf("HB: 0x%02X\n", HI_BYTE($test));

$test = MAKE_WORD(LO_BYTE($test), HI_BYTE($test));
echo sprintf("0x%04X\n", $test);

echo sprintf("LB: 0x%02X\n", LO_BYTE($test));
echo sprintf("HB: 0x%02X\n", HI_BYTE($test));

$test = 0x12345678;
echo sprintf("0x%08X", $test);

echo sprintf("LW: 0x%04X\n", LO_WORD($test));
echo sprintf("HW: 0x%04X\n", HI_WORD($test));

$test = MAKE_LONG(LO_WORD($test), HI_WORD($test));
echo sprintf("0x%04X", $test);

echo sprintf("LW: 0x%04X\n", LO_WORD($test));
echo sprintf("HW: 0x%04X\n", HI_WORD($test));

$test = 0x1234;
echo sprintf("Start: 0x%04X\n", $test);
echo "Array: ";
$arr = array(LO_BYTE($test), HI_BYTE($test));
foreach ($arr as $a)
	echo sprintf("0x%02X, ", $a);
echo "\n";
echo "Array: ".implode(", ", $arr)."\n";
echo "String: ";
$str = '';
foreach ($arr as $a)
{
	$str .= sprintf("%c", $a);
}
echo $str."\n";
echo "Hex output\n";
echo hexoutput($str);

echo '</pre>';
?>

Open in new window

0
 

Author Comment

by:j-tech
ID: 33533377
Thanks TDS, that worked perfectly.  Also helped with converting an integer to a single byte array to single character string.  Appreciate it!
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question