Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASCII-code generator

Status
Not open for further replies.
Joined
Aug 1, 2004
Messages
1
Location
BE
I am building an ASCII-code generator that makes all possible combinations of 3 ASCII characters starting from char 33.

when I run the code, the second charactor stops at '<' that is character number 60, my for loops are correctly declared I think... explorer gives errors because it is possible that internet explorer will react slow while executing the script, but i always click 'no', so the script wont be aborted...

here is the code:

<script language="JavaScript"> <!--


function combination() {

var combination ='';


for(Ca=33;Ca<128;Ca++)
{
for(Cb=33;Cb<128;Cb++)
{
for(Cc=33;Cc<128;Cc++)
{

combination=(String.fromCharCode(Ca))+(String.fromCharCode(Cb))+(String.fromCharCode(Cc));
document.write(combination+'<br>');
}
}
}

}

//-->
</script>

can somebody please help me!..!..! ??
thnx in advance
PS: indeed English is not my motherlanguage, excuse me for the spelling and grammar mistakes ;)
 
ur script cause my IE to throw up an error that said it is not safe to execute this script as it takes too much memory...

Known is handfull, Unknown is worldfull
 
Some math:

128 - 33 = 95
95*95*95 = 857375

Browser takes 2 DOM nodes to represent each row (one for text, another for BR):
857375*2 = 1714750 objects.

My machine went bonkers for 20 seconds. :(

Anyway, "<", ">", "&" and """ are HTML entity characters. They are probably in source but output is screwed - usually everything between "<" and ">" is considered tag and therefore not displayed. Replace entity chars with "&lt;", "&gt;", "&amp;" and "&quot;" respectively.
 
The following version of the script is more optimised. I have added some fun benchmarking code as well so you can see how long it took to do each "run" of data (in milliseconds).

Code:
<script type="text/javascript">
function combination(CaMax,CbMax,CcMax)
{
	var startTime = new Date().getTime();
	var combination='';
	for(Ca=33;Ca<CaMax;Ca++)
	{
		for(Cb=33;Cb<CbMax;Cb++)
		{
			for(Cc=33;Cc<CcMax;Cc++)
			{
				combination+=String.fromCharCode(Ca);
				combination+=String.fromCharCode(Cb);
				combination+=String.fromCharCode(Cc);
				combination+='<br />';
			}
		}
	}
	var endTime = new Date().getTime();
	document.write('Time to complete request: '+(endTime-startTime)+' milliseconds<br /><br />');
	document.write(combination);
}
</script>

combination(34,64,128) -- Time to complete request: 8533 milliseconds
combination(34,128,128) -- Time to complete request: 85186 milliseconds

I did this running on MacOSX Safari. Anyone else want to post some of their numbers?

I'll leave the combination(128,128,128) running overnight and see if the Mac can withstand the load *lol*

Jeff
 
The way high-level languages work, mass string concatenation isn't good thing. Here are my results:

34,64,128 = 328ms
34,128,128 = 2891ms
64,128,128 = 321722ms
128,128,128 = not responding :(

I tried to flush string into array after each Ca loop and here are results:

34,64,128 = 203ms
34,128,128 = 2828ms
64,128,128 = 115765ms
128,128,128 = 306234ms

With original code (direct document.write in innermost loop):

34,64,128 = 94ms
34,128,128 = 219ms
64,128,128 = 7594ms
128,128,128 = 22204ms
 

My advice? Write this program in C, Pascal, VB, etc, and write the output directly to a text file - it'll be infinitely quicker (and that includes the time taken to install those programs, I'd say).

Can you enlighten us as to why you want this code? Maybe there would be another way of getting the desired result.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top