Web Analytics Made Easy -
StatCounter Faster Replace Of Chars (ASCII) to Dec - CodingForum

Announcement

Collapse
No announcement yet.

Faster Replace Of Chars (ASCII) to Dec

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

  • Faster Replace Of Chars (ASCII) to Dec

    Hello,

    Currently I am using the below script to replace all 256 ASCII chars with their known hex codes and a * infront of each, I know there's some missing, but it skips them anyway

    for (z=1;z<=256;z++) {
    chr = "\\"+String.fromCharCode(z);
    re = new RegExp(chr,'gi');
    strTemp = strTemp.replace(re,"*"+z);
    }

    strTemp is obviously the variable I start with. Right now, if I have a string that is more than 1 line (slight exaggeration, really a bit more that that), the script freezes. And even when it is only 1 line, it's really slow.

    I know why it's doing this, because of the loop and that having to cycle through all of the Regular Expression handling.

    Is there an absolute faster way? I can't use a straight-out ASCII to Dec convertor because I need to have a * infront of each changed value, or can I still use a convertor and just mod it to do that?

    Any help is MUCH appreciated, thanks.

  • #2
    Also, when I do this, I get flooded with "119"'s rather than the actual converted values, does anyone know why?

    Comment


    • #3
      It's sort of difficult to understand that description, but maybe something like this is close to an answer:

      var hex = "*" + z.toString(16).toUpperCase();
      hmm... ?

      Comment


      • #4
        Do I replace the fromCharCode line with that or...?

        Comment


        • #5
          If you're just trying convert (#z) to ("*" + hex), that's probably about all you'd need in there...
          hmm... ?

          Comment


          • #6
            Code:
            var newStr = "";
            for(var i = 0; i < strTemp.length; i++)
            	newStr += "*" + (strTemp.charCodeAt(i)).toString(16);
            Hope that helps!

            Happy coding!

            Comment


            • #7
              Thank you all for your help...

              It's close... But no cigar unfortunately. Please assist:

              nolachrymose showed exactly what I was hoping for, a function which replicates PHP's bin2hex. The only problem was, when I used code (I'll just call it hex2bin because that's what it does, reverses bin2hex), some things were replaced with the wrong values, I'm thinking.

              I *think* it's a problem with the JavaScript because I tested the hex2bin on the PHP bin2hex of the code and it returned the original value successfully.

              Ok, here's what's happening...

              The original source file:
              Code:
              <!-- START MAIN CONTENT -->
              <TABLE WIDTH="600" BORDER="0" CELLPADDING="0" CELLSPACING="0">
              	<TR>
              		<TD WIDTH="600" VALIGN="top" ALIGN="CENTER">
              			<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"  WIDTH="480" HEIGHT="244" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">
              			<PARAM NAME="data" VALUE="http://a772.g.akamai.net/5/772/51/fe463836e7c745/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81788fc5bcb3aad2186f86b/qtstart5a_480x228.mov">
              			<PARAM NAME="controller" VALUE="true">
              			<PARAM NAME="target" VALUE="myself">
              			<PARAM NAME="autoplay" VALUE="true">
              			<PARAM NAME="autostart" VALUE="true">
              			<PARAM NAME="src" VALUE="http://a772.g.akamai.net/5/772/51/5c1b90a1bf793d/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81785ea61ce2fa10c71e51488f055c526990c75e452c1279b0368cb33a50e81f5/thepassionofthechrist-sref.mov">
              			<PARAM NAME="pluginspage" VALUE="http://www.apple.com/quicktime/download/indext.html">
              			<EMBED WIDTH="480" HEIGHT="244" CONTROLLER="false" TARGET="myself" HREF="http://a772.g.akamai.net/5/772/51/5c1b90a1bf793d/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81785ea61ce2fa10c71e51488f055c526990c75e452c1279b0368cb33a50e81f5/thepassionofthechrist-sref.mov" SRC="http://a772.g.akamai.net/5/772/51/fe463836e7c745/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81788fc5bcb3aad2186f86b/qtstart5a_480x228.mov" BGCOLOR="FFFFFF" BORDER="0" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.html"></EMBED>		
              			</OBJECT>		
              		</TD>
              	</TR>
              </TABLE>
              <!-- END MAIN CONTENT -->
              Then I used nolachrymose's code (first the JavaScript split the above source by lines and then nolachrymose's code and a toUpperCase() is applied to each line, perhaps this is where the problem is?) and this was the output:
              Code:
              3C212D2D205354415254204D41494E20434F4E54454E54202D2D3E3C5441424C452057494454483D223630302220424F524445523D2230222043454C4C50414444494E473D2230222043454C4C53504143494E473D2230223E93C54523E993C54442057494454483D22363030222056414C49474E3D22746F702220414C49474E3D2243454E544552223E9993C4F424A45435420434C41535349443D22636C7369643A30324246323544352D384331372D344232332D424338302D44333438384142444443364222202057494454483D2234383022204845494748543D223234342220434F4445424153453D22687474703A2F2F7777772E6170706C652E636F6D2F7174616374697665782F7174706C7567696E2E636162223E9993C504152414D204E414D453D2264617461222056414C55453D22687474703A2F2F613737322E672E616B616D61692E6E65742F352F3737322F35312F66653436333833366537633734352F31613161316161613231393863363237393730373733643830363639643834353734613864383064336362313234353363303235383966323533383266363638633933323965303337356538313738386663356263623361616432313836663836622F7174737461727435615F343830783232382E6D6F76223E9993C504152414D204E414D453D22636F6E74726F6C6C6572222056414C55453D2274727565223E9993C504152414D204E414D453D22746172676574222056414C55453D226D7973656C66223E9993C504152414D204E414D453D226175746F706C6179222056414C55453D2274727565223E9993C504152414D204E414D453D226175746F7374617274222056414C55453D2274727565223E9993C504152414D204E414D453D22737263222056414C55453D22687474703A2F2F613737322E672E616B616D61692E6E65742F352F3737322F35312F35633162393061316266373933642F31613161316161613231393863363237393730373733643830363639643834353734613864383064336362313234353363303235383966323533383266363638633933323965303337356538313738356561363163653266613130633731653531343838663035356335323639393063373565343532633132373962303336386362333361353065383166352F74686570617373696F6E6F667468656368726973742D737265662E6D6F76223E9993C504152414D204E414D453D22706C7567696E7370616765222056414C55453D22687474703A2F2F7777772E6170706C652E636F6D2F717569636B74696D652F646F776E6C6F61642F696E646578742E68746D6C223E9993C454D4245442057494454483D2234383022204845494748543D223234342220434F4E54524F4C4C45523D2266616C736522205441524745543D226D7973656C662220485245463D22687474703A2F2F613737322E672E616B616D61692E6E65742F352F3737322F35312F35633162393061316266373933642F31613161316161613231393863363237393730373733643830363639643834353734613864383064336362313234353363303235383966323533383266363638633933323965303337356538313738356561363163653266613130633731653531343838663035356335323639393063373565343532633132373962303336386362333361353065383166352F74686570617373696F6E6F667468656368726973742D737265662E6D6F7622205352433D22687474703A2F2F613737322E672E616B616D61692E6E65742F352F3737322F35312F66653436333833366537633734352F31613161316161613231393863363237393730373733643830363639643834353734613864383064336362313234353363303235383966323533383266363638633933323965303337356538313738386663356263623361616432313836663836622F7174737461727435615F343830783232382E6D6F7622204247434F4C4F523D224646464646462220424F524445523D22302220504C5547494E53504147453D22687474703A2F2F7777772E6170706C652E636F6D2F717569636B74696D652F646F776E6C6F61642F696E646578742E68746D6C223E3C2F454D4245443E999993C2F4F424A4543543E99993C2F54443E93C2F54523E3C2F5441424C453E3C212D2D20454E44204D41494E20434F4E54454E54202D2D3E
              Then when I use the custom hex2bin command, returned is:
              Code:
              <!-- START MAIN CONTENT --><TABLE WIDTH="600" BORDER="0" CELLPADDING="0" CELLSPACING="0">“إE#é“إDBt”EDƒز#c"dؤ”tمز'F÷"ؤ”tمز$4TهDU"#é™<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"  WIDTH="480" HEIGHT="244" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">™“إ$زنشSز&FF"dإTSز&‡GG¢ٍِss"وrو¶ض’ووWBَRَss"َSِfSCc3ƒ3fSv3sCRَ#“†3c#s“sss6Cƒcc–CƒCSsF†CƒC66##CS63#Sƒ–c#S3ƒ&ccc†3“3#–S3sVSƒsƒ†f3V&6#6C#ƒfcƒf"÷G7F'CVَCƒƒ##‚وض÷b#é™<PARAM NAME="controller" VALUE="true">™“إ$زنشSز'F&vWB"dإTSز&×—6Vئb#é™<PARAM NAME="autoplay" VALUE="true">™“إ$زنشSز&WF÷7F'B"dإTSز'G'VR#é™<PARAM NAME="src" VALUE="http://a772.g.akamai.net/5/772/51/5c1b90a1bf793d/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81785ea61ce2fa10c71e51488f055c526990c75e452c1279b0368cb33a50e81f5/thepassionofthechrist-sref.mov">™“إ$زنشSز'اVv–ç7vR"dإTSز&‡GG¢ٍ÷wwrوئRو6ِز÷V–6·F–ضRِF÷vوئِBِ–وFW‡Bو‡Fضآ#é™<EMBED WIDTH="480" HEIGHT="244" CONTROLLER="false" TARGET="myself" HREF="http://a772.g.akamai.net/5/772/51/5c1b90a1bf793d/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81785ea61ce2fa10c71e51488f055c526990c75e452c1279b0368cb33a50e81f5/thepassionofthechrist-sref.mov" SRC="http://a772.g.akamai.net/5/772/51/fe463836e7c745/1a1a1aaa2198c627970773d80669d84574a8d80d3cb12453c02589f25382f668c9329e0375e81788fc5bcb3aad2186f86b/qtstart5a_480x228.mov" BGCOLOR="FFFFFF" BORDER="0" PLUGINSPAGE="http://www.apple.com/quicktime/download/indext.html"></EMBED>™™“آôô$¤T5C陓آُDCé</TR></TABLE><!-- END MAIN CONTENT -->
              Notice how the majority of the original source is returned, yet there are still some wrong values?

              Thank you SO much for any help!

              Comment


              • #8
                Found Problem

                I found the problem, there are excess 9's inbetween some of the 3C's and 3E's (a.k.a. < and >)

                Why are those escess 9's there? How do I fix that without stripping all 9's (if I do that, I'll lose some other chars which have 9's in the hex)?

                Thanks!

                Comment


                • #9
                  If you want to render that source string from hex values, each would need to be prefixed with \x ("\\x"), (or "\\x0" if less than two digits)...
                  Last edited by swmr; Mar 3, 2004, 05:09 AM.
                  hmm... ?

                  Comment

                  Working...
                  X