Web Analytics Made Easy -
StatCounter Source of PHP's bin2hex command? - CodingForum

Announcement

Collapse
No announcement yet.

Source of PHP's bin2hex command?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Source of PHP's bin2hex command?

    Hello,

    Does anyone know how PHP's bin2hex command works? I found a code to reverse it (hex2bin shown below), now I'm just hoping there is a similar code for bin2hex :

    function hex2bin($hexdata) {

    for ($i=0;$i<strlen($hexdata);$i+=2) {
    $bindata.=chr(hexdec(substr($hexdata,$i,2)));
    }

    return $bindata;
    }

    I need to find the source of PHP's bin2hex so I can translate it into JavaScript. JS's capabilities only allow me to bin2hex numbers whilst PHP's let's me bin2hex just about anything, even the raw source of a .EXE application.

    I'm trying to preserve a variable (the source of a .EXE application) and get it from JavaScript into PHP and I think this is the only way considering every other way I tried lost a lot of the special characters. If you can think of another way, please let me know.

    Thanks for any help in advance.

  • #2
    You asked for it, you get it: The source of bin2hex():

    Code:
    /* {{{ proto string bin2hex(string data)
       Converts the binary representation of data to hex */
    PHP_FUNCTION(bin2hex)
    {
    	zval **data;
    	char *result;
    	size_t newlen;
    
    	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &data) == FAILURE) {
    		WRONG_PARAM_COUNT;
    	}
    	convert_to_string_ex(data);
    
    	result = php_bin2hex(Z_STRVAL_PP(data), Z_STRLEN_PP(data), &newlen);
    	
    	if (!result) {
    		RETURN_FALSE;
    	}
    
    	RETURN_STRINGL(result, newlen, 0);
    }
    /* }}} */
    Taken from http://cvs.php.net/co.php/php-src/ex...ogin=2&r=1.412

    You'll have to get familiar with the internal ZEND functions, I'm afraid. Regarding your problem - I did not understand much. You're trying to convert the source of an .exe file by Javascript and get that into PHP, is that right? But what for? What shall a PHP script do with that encoded source? And how does Javascript read the source file?

    So many question marks...
    De gustibus non est disputandum.

    Comment


    • #3
      Thanks for that (scary looking, haha).

      This is exactly what I am hoping to do:

      JavaScript uses it's ActiveX object to open a file stream to a .EXE the user selects. [done]
      JavaScript reads this enormous file (well... not enormous...) into 50KB "packets" (strings). [done]
      JavaScript does SOMETHING to these 50KB string (because otherwise I lose all of the special characters that were in the .EXE source, making it unusable) to make me able to pass them on (one by one) to PHP via an IMG tag, Script tag, etc. [help please ]
      PHP (which was called via an IMG, Script, etc. tag) reads the 50KB variable it was passed, reverses whatever JavaScript changed it to, and starts to "parse" it all one packet at a time [can easily do this once I get that JavaScript part]
      PHP then uses gzcompress to compress the packets for storage in text files. [done]

      In essence, I'm trying to make a faster (it is so far) alternative to uploading the entire file to the server. I also am doing this packet alternative because I don't want to host huge .EXE files on my server. The cool thing I found was that when I merge the packets so multiple users can simultaneously download it, my server's memory is about 1/15th what it would be if they were downloading the entire .EXE file.

      Thanks for any further help.

      Comment


      • #4
        Ok, you're trying desparately to avoid any FTP clients.

        But the part of splitting the big file into smaller chunks to optimize memory consumption sounds good.

        Originally posted by javacookman
        JavaScript does SOMETHING to these 50KB string (because otherwise I lose all of the special characters that were in the .EXE source, making it unusable) to make me able to pass them on (one by one) to PHP via an IMG tag, Script tag, etc. [help please ]
        For the SOMETHING part, you needed the bin2hex source, right? To encode the binary before submitting it to the PHP script.

        I'm not totally sure it would work to append a 50KB string to the <img> tag's href attribute, that would result in an awfully long query string that most certainly would be chopped by the browser.

        There is a possibility to send XML data over HTTP for newer browsers, and there's a nice library that eases the handling of this. Although, it seems you're targetting only IE as platform, so could just use it's native XMLHTTP objects to send the data. Whether that does work or not... might depend if you manage to get the data encoded before stuffing it into a XML request. Anyway, here are some links:




        Good luck!
        De gustibus non est disputandum.

        Comment

        Working...
        X