Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

unpack error

Hi,

when I converted php to c# using phpconvert tool, I got the following error. how can write a similar function of unpack in c#?

data = PHP.FileSystemSupport.Read(fp, 4);
      //CONVERSION_ISSUE: Method 'unpack' was not converted.
      numOfFacets = unpack("I", data);

Please help.

ayha
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Avatar of ayha1999
ayha1999

ASKER

Hi,

In the PHP unpack functions accepts two parameters but in the c# custom function only one parameter. could you please modifiy the code.

Public Shared Function unpack(str As String) As String

    Dim x As Integer
    Dim rstStr As String = ""
    For x = 0 To str.Length - 1
        rstStr &= Convert.ToString(Asc(str.Substring(x, 1)), 16)
    Next

    Return rstStr
End Function

thanks
when I converted php to c#
I am confused your question talkes about C# but you are posting VB code?
sorry,  here is the c# code

public static string unpack(string str)
{

      int x = 0;
      string rstStr = "";
      for (x = 0; x <= str.Length - 1; x++) {
            rstStr += Convert.ToString(Strings.Asc(str.Substring(x, 1)), 16);
      }

      return rstStr;
}
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America 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
Hi Ray,

Actually I want c# custom function to replace php pack function.
Sorry, I misunderstood.  Good luck with it, ~Ray
Did you look at the option of defining a struct against the data you want to unapck?
greetings  ayha1999, , you do not state what the "Conversion" result should be for your Unpack function, it looks to me that all you want to do is change an english string like "abc" to HEX as  "616263", is this what your unpack function is suppose to do?  But maybe I do not see what the second parameter in -
ToString(Strings.Asc(str.Substring(x, 1)), 16) // the 16 parameter

is for, I guess the 16 is for HEX output?
when I try to convert php to c# using phpconverter, the unpack function as not converted. the full php code is here.

<?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, '.', ',');


?>

Open in new window


ayha
SOLUTION
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
To follow - the 2 byte byte count is never used so there is no need to unpack this value - it can be discarded with a simple 2 byte file pointer advancement.
SOLUTION
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
Correction on my earlier post - the data in the file for the vertices are 32 bit floats not Int values so converting to Int is not the right solution here.

Reading 4 bytes - reversing if LittleEndian and then BitConvert.ToSingle - seems to do the trick.