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
LVL 7
ayha1999Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
In PHP, everything is documented in the online man pages.  Example:
http://php.net/manual/en/function.unpack.php

Formatting codes (first input argument to unpack() function) are shown here:
http://php.net/manual/en/function.pack.php

PHP is an extensible language and you can write your own functions.  In the function definition you can provide a default value for any of the arguments.  Example:
<?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);

Open in new window

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));

Open in new window

Best regards, ~Ray
0
 
ayha1999Author Commented:
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Julian HansenCommented:
when I converted php to c#
I am confused your question talkes about C# but you are posting VB code?
0
 
ayha1999Author Commented:
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;
}
0
 
ayha1999Author Commented:
Hi Ray,

Actually I want c# custom function to replace php pack function.
0
 
Ray PaseurCommented:
Sorry, I misunderstood.  Good luck with it, ~Ray
0
 
Julian HansenCommented:
Did you look at the option of defining a struct against the data you want to unapck?
0
 
Slick812Commented:
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?
0
 
ayha1999Author Commented:
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
0
 
Julian HansenConnect With a Mentor Commented:
The unpacking in this code is doing the same thing as the read_32bitInt function here http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28149820.html#a39227987

An STL file has an 80 byte header followed by a 4byte Int indicating the number of triangles in the file. After that follows a 50byte block for each triangle made up of 12 vectors (xyz) for the normal and 3 vertices of the triangle. The remaining 2 bytes is an int indicating a byte count (usually 0).

So in this case all the unpack is doing is converting the 2/4 byte values read from the file to an int and making allowance for BigEndian / LittleEndian conversion where necessary.
0
 
Julian HansenCommented:
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.
0
 
Slick812Connect With a Mentor Commented:
There are LARGE differences in the programming languages of PHP and C, (including C++ and C#) for dealing with File data that is NOT Text (binary). PHP is more or less stuck with a "string" type for all data from sources like files, but C, C++ and C# is Not limited to strings, and can use better, faster types File access including Byte-arrays and "byte-pointer or unsigned-integer-pointer" access and positioning and can do BitConverter or type-Casting to easily get short, integer,  unsigned-integer, real or character "type" from a file (memory) pointer position. I am sure that you know little about programming in C#, so what I just said does not make much sense to you, But My meaning for you is - that using the phpconvert tool, you can not expect it to get correct code translations for rather complex PHP functions like unpack( ) using the "String" methods of PHP.  julianH has given you some ideas for using C# methods, but you may not understand any of that, but it would require an entire re-write of the code block you have in PHP using NON-STRING file access..

I might suggest that if you need to use the phpconvert tool, you might consider NOT using the PHP unpack function to get integer values from a 4 character string. . I would suggest you try using this function Str2Int( ) as it may be better for the phpconvert tool to convert -
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);

Open in new window

0
 
Julian HansenCommented:
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.
0
Question has a verified solution.

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.

All Courses

From novice to tech pro — start learning today.