?
Solved

unpack error

Posted on 2013-06-05
14
Medium Priority
?
383 Views
Last Modified: 2013-06-12
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
0
Comment
Question by:ayha1999
[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
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 7

Author Comment

by:ayha1999
ID: 39224835
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39224870
when I converted php to c#
I am confused your question talkes about C# but you are posting VB code?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:ayha1999
ID: 39224912
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 536 total points
ID: 39225560
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
 
LVL 7

Author Comment

by:ayha1999
ID: 39226056
Hi Ray,

Actually I want c# custom function to replace php pack function.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39226262
Sorry, I misunderstood.  Good luck with it, ~Ray
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39226274
Did you look at the option of defining a struct against the data you want to unapck?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 39227250
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
 
LVL 7

Author Comment

by:ayha1999
ID: 39227975
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
 
LVL 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 532 total points
ID: 39228242
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39228244
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
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 532 total points
ID: 39229826
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 39230627
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

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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