Please help converting 2 PHP functions to vb.net (or c#)
I'm converting a php code containing the following 2 functions:
PHP Code:
/**
* Covert a string into longinteger
*
* @param string $data
* @return array
*/
function _str2long($data)
{
$n = strlen($data);
$tmp = unpack('N*', $data);
$data_long = array();
$j = 0;
foreach ($tmp as $value) $data_long[$j++] = $value;
return $data_long;
}
/**
* Convert a longinteger into a string
*
* @param int $l
* @return string
*/
function _long2str($l)
{
return pack('N', $l);
}
I am currently concerned with php pack and unpack functions. Although I've got some rough understanding about what they are doing I am a bit worried if I understand them right and I've no test data to ensure I'll make a correct implementation. Can anyone help me to convert this code or basically, what I want is to get a correctly working implementation of php pack and upack functions with this exact format ("N" and "N*") in either vb.net or c#.
Re: Please help converting 2 PHP functions to vb.net (or c#)
Hi.
First: I have no knowledge whatsoever of PHP.
But: Your question seems to be asking how to get the value of a String variable into a Long variable, and how to produce a String variable from a Long in VB.NET.
Certainly a String from a Long is straightforward enough: String_Name = Long_Name.ToString,
and the reverse is equally simple: Long_Name = Val(String_Name). Where "...Name" is the name of the relevant variable.
These will also work for Integers, Singles, Doubles etc. for both positive and negative values. For Floating values these will also convert any decimal value correctly.
Poppa.
Re: Please help converting 2 PHP functions to vb.net (or c#)
I think you should try and understand how Unpack and pack work. The documentation for each function (unpack and [http://php.net/manual/en/function.pack.php]pack[/url]) is a wonky explanation to me at best...I don't think Poppa understood exactly what they did. It's...interesting. May I ask why you want to do this cicatrix? There might be a better way to do things if I had a wee bit of background info.
Re: Please help converting 2 PHP functions to vb.net (or c#)
After spending some time reading up on Pack, I came up with this:-
vbnet Code:
'
Private Function Pack_N(ByVal I32 As Integer) As String
Dim bytes As Byte() = BitConverter.GetBytes(I32)
Dim r As String = ""
For Each B As Byte In bytes.Reverse
r += Chr(B)
Next
Return r
End Function
Now I'm only about 60% sure that "Pack('N')" does something like that and I can't test it since as far as I know, I don't have a PHP interpreter/compiler or whatever.
Also, to clear up any confusion about what Pack is for, it seems to me that it was thought up to create structures on the fly. Now this is my understanding of it based on what I could decipher from info about it on the internet.