Need Help Understanding this little bit of javascript.

Discussion in 'JavaScript' started by HellBomb, May 15, 2011.

  1. #1
    Ok, i found this online as a way to make certain strings interact with certain web pages, but can anyone help me break it down a little?
         function Populate()
         {
              hex = new MakeArray();
              hex[1] = "0";
              hex[2] = "1";
              hex[3] = "2";
              hex[4] = "3";
              hex[5] = "4";
              hex[6] = "5";
              hex[7] = "6";
              hex[8] = "7";
              hex[9] = "8";
              hex[10] = "9";
              hex[11] = "a";
              hex[12] = "b";
              hex[13] = "c";
              hex[14] = "d";
              hex[15] = "e";
              hex[16] = "f";
         }
    
         function get_hex(N)
         {
              s = "";
              A = N;
              while (A >= 16) {
                   B = A % 16;
                   A = Math.floor(A / 16);
                   s = s + hex[B+1];
              }
              s += hex[A+1];
    
              return transpose(s);
         }
    
         function transpose(s)
         {
              N = s.length;
              t = "";
              for (i = 0; i < N; i++) {
                   t = t + s.substring(N-i-1, N-i);
              }
    
              //   zero-pad
              s = t;
              for (i = 0; i < (5 - t.length); i++) {
                   s = "0" + s;
              }
    
              return s;
         }
    Code (markup):

     
    HellBomb, May 15, 2011 IP
  2. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    My math is a bit rusty but that looks like a terrible way to convert a base 10 number into a base 16 number. He's basically converting a number into its hex code but it's done really poorly.
     
    AntelopeSalad, May 15, 2011 IP
  3. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #3
    interesting, I have no idea on how it works :p but that will help me figure it out.
     
    HellBomb, May 16, 2011 IP
  4. KittyS

    KittyS Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, so here's whats going on:

    Populate creates an array of these numbers. Get_hex uses these numbers to do some conversion and return a transposed hex code. Finally, transpose does some stuff to that hex code and spits out a variable "s". Maybe changing hex codes?

    Stackoverflow.com has a lot of programmers that are willing to help too.
     
    KittyS, May 16, 2011 IP
  5. AntelopeSalad

    AntelopeSalad Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    AntelopeSalad, May 16, 2011 IP
  6. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It starts with a decimal number (N) and an empty result (s).
    It uses the modulus operator (%) to get the remainder of the number divided by 16.
    It then looks up the hex value of the remainder and adds it to the result in sequence.
    The number is then divided by 16 and the loop starts again.
    Once the number is under 16, the loop stops and the final hex code is added.
    The string is then transposed (reversed) and leading zeros added if it's too short.

    Try this with a really large number, like 10000.
    
    <html>
    <head>
    <script type="text/javascript">
    	hex = new Array();
    	hex[1] = "0";
    	hex[2] = "1";
    	hex[3] = "2";
    	hex[4] = "3";
    	hex[5] = "4";
    	hex[6] = "5";
    	hex[7] = "6";
    	hex[8] = "7";
    	hex[9] = "8";
    	hex[10] = "9";
    	hex[11] = "a";
    	hex[12] = "b";
    	hex[13] = "c";
    	hex[14] = "d";
    	hex[15] = "e";
    	hex[16] = "f";
    
    	function get_hex(N)	{	
    		s = ""; // start with an empty answer string
    		A = N; // create a copy of the number
    		while (A >= 16) { // keep looping while the number is greater or equal to 16
    			B = A % 16; // get the remainder of the number divided by 16
    			A = Math.floor(A / 16); // divide the number by 16 and save as the new number
    			s = s + hex[B+1]; // add the hex code for the remainder to the string
    			addAnswer(s); // write answer to the page
    		}
    		s += hex[A+1]; // now that the number is less than 16, add it's hex code to the string.
    		addAnswer(s);  // write answer to the page
    		return transpose(s); // return the reversed string with leading zeros added
    	}
    
    	function transpose(s)	{
    		N = s.length;
    		t = "";
    		for (i = 0; i < N; i++) { // move from end to the start copying characters to a new string
    			t = t + s.substring(N-i-1, N-i);
    		}
    		s = t;
    		for (i = 0; i < (5 - t.length); i++) { // add zeros to the front if string less than five
    			s = "0" + s;
    		}
    		return s;
    	}
    	
    	function convert() {
    		var dec = document.getElementById('number').value;
    		var	hex = get_hex(dec);
    		addAnswer(hex);
    	}
    	
    	function addAnswer(n) {
    		var answer = document.getElementById('answer').innerHTML;
    		document.getElementById('answer').innerHTML = answer + '<br />' + n;
    	}
    
    </script>
    <style type='text/css'>
    input, p { display: block; margin: 10px 0; }
    </style>
    </head>
    <body>
    <input id='number' type='text' />
    <input type='button' onclick='convert()' value='Convert' />
    <div id='answer'></div>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, May 16, 2011 IP
  7. HellBomb

    HellBomb Active Member

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    80
    #7
    anyone think they could write a little piece of javascript that could effectively reverse it? I know the last 3 numbers are not coded because that is the password of the user when inputted. I may be able to swing a few dollars your say if you can, but no guarantees. If you can you can use the number 112602 as N as i know that is a valid number for the entry.
     
    HellBomb, May 17, 2011 IP
  8. KittyS

    KittyS Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    HellBomb,

    Why do you think that a programmer would do this for free?

    If you need programming help, hire them or at least offer something in kind - not say "but no guarantees". I don't expect YOU to bomb hell for free!
     
    KittyS, May 17, 2011 IP
  9. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Wow, this thread just got very dodgy. LOL :D
     
    Cash Nebula, May 17, 2011 IP