Results 1 to 4 of 4

Thread: Please help converting 2 PHP functions to vb.net (or c#)

  1. #1

    Thread Starter
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Question 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#.

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,500

    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.
    Last edited by Poppa Mintin; Jun 6th, 2013 at 10:40 AM.
    Along with the sunshine there has to be a little rain sometime.

  3. #3

    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.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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:
    1. '
    2.     Private Function Pack_N(ByVal I32 As Integer) As String
    3.  
    4.         Dim bytes As Byte() = BitConverter.GetBytes(I32)
    5.         Dim r As String = ""
    6.  
    7.         For Each B As Byte In bytes.Reverse
    8.             r += Chr(B)
    9.         Next
    10.  
    11.         Return r
    12.     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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width