The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!
<?php // RAY_temp_ayha1999.php
error_reporting(E_ALL);
echo '<pre>';
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28149245.html
// SOME EXAMPLES IN NATIVE PHP
$x = '78';
$y = pack("H*", $x);
$z = ord($y);
$a = unpack("H*", $y);
var_dump($x, $y, $z, $a);
// CREATING AN ALTERNATIVE FUNCTION
function myUnpack($value, $code="H*")
{
return unpack($code, $value);
}
// TEST THE FUNCTION USING ONLY ONE INPUT ARGUMENT
$a = MyUnPack($y);
var_dump($a);
So the key decision remaining for you would be the determination of the formatting code that you want to use as the default value. I chose H* because it seems to be the most frequent code appearing in my library of teaching examples, but I do not know whether it is appropriate for your needs. Note that unpack() returns an array. If you need a string you can use something like implode(NULL, $array); which can be added to the return statement in the function. It might look something like this:return implode(NULL, unpack($code, $value));
Best regards, ~Ray
<?php
$x_max = 0;
$y_max = 0;
$z_max = 0;
$x_min = 0;
$y_min = 0;
$z_min = 0;
$filepath = "D:\wamp\www\g.stl";
$fp = fopen($filepath, "rb");
$section = file_get_contents($filepath, NULL, NULL, 0, 79);
fseek($fp, 80);
$data = fread($fp, 4);
$numOfFacets = unpack("I", $data);
for ($i = 0; $i < $numOfFacets[1]; $i++){
//Start Normal Vector
$data = fread($fp, 4);
$hold = unpack("f", $data);
$normalVectorsX[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$normalVectorsY[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$normalVectorsZ[$i] = $hold[1];
//End Normal Vector
//Start Vertex1
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex1X[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex1Y[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex1Z[$i] = $hold[1];
//End Vertex1
//Start Vertex2
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex2X[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex2Y[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex2Z[$i] = $hold[1];
//End Vertex2
//Start Vertex3
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex3X[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex3Y[$i] = $hold[1];
$data = fread($fp, 4);
$hold = unpack("f", $data);
$vertex3Z[$i] = $hold[1];
//End Vertex3
//Attribute Byte Count
$data = fread($fp, 2);
$hold = unpack("S", $data);
$abc[$i] = $hold[1];
$x_vals = array($vertex1X[$i], $vertex2X[$i], $vertex3X[$i]);
$y_vals = array($vertex1Y[$i], $vertex2Y[$i], $vertex3Y[$i]);
$z_vals = array($vertex1Z[$i], $vertex2Z[$i], $vertex3Z[$i]);
if(max($x_vals) > $x_max) {
$x_max = max($x_vals);
}
if(max($y_vals) > $y_max) {
$y_max = max($y_vals);
}
if(max($z_vals) > $z_max) {
$z_max = max($z_vals);
}
if(min($x_vals) < $x_min) {
$x_min = min($x_vals);
}
if(min($y_vals) < $y_min) {
$y_min = min($y_vals);
}
if(min($z_vals) < $z_min) {
$z_min = min($z_vals);
}
}
$x_dim = $x_max - $x_min;
$y_dim = $y_max - $y_min;
$z_dim = $z_max - $z_min;
$volume = $x_dim*$y_dim*$z_dim;
$raw_cost = 15;
$tray_cost = $raw_cost;
$material_cost = $raw_cost*$volume*1.02;
$support_cost = $raw_cost*2;
$total = $tray_cost + $material_cost + $support_cost;
echo "$".number_format($total, 2, '.', ',');
?>
function Str2Int($str1){
if (strlen($str1) < 4) return 0;
$int = ord($str1[0]) | (ord($str1[1])<<8) | (ord($str1[2])<<16) | (ord($str1[3])<<24);
return $int;
}
// and in your code substitute the unpack for this -
$data = fread($fp, 4);
$int = Str2Int($data);
$normalVectorsX[$i] = $int ;
// OR shorter
$data = fread($fp, 4);
$normalVectorsX[$i] = Str2Int($data);
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.