Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 36

Thread: [DHTML] ActiveX Simplified

  1. #11
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Haha.

    I'll be wondering what's keeping him busy til he arrives
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #12
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Here are better versions of your text converters:
    Code:
    function $ToBinary(text) {
    	var binText = "", ch = "", bin;
    	for(var i=0, n=text.length; i<n; ++i) {
    		ch = text.charAt(i);
    		bin = ch.charCodeAt(0).toString(2);
    		while(bin.length<8) bin = "0" + bin;
    		binText += bin;
    		}
    	return binText;
    	}
    
    function $ToAscii(binText) {
    	var text = "", bin, dec;
    	if (binText.length&#37;8!=0) return false;
    	for(var i=0, n=binText.length/8; i<n; ++i) {
    		bin = binText.substr(8*i, 8);
    		dec = 0;
    		for(var j=0; j<8; ++j) dec += Math.pow(2, j) * bin.charAt(7-j);
    		text += String.fromCharCode(dec);
    		}
    	return text;
    	}

    You could also support full unicode if you wanted:
    Code:
    function $ToBinary(text) {
    	var binText = "", ch = "", bin;
    	for(var i=0, n=text.length; i<n; ++i) {
    		ch = text.charAt(i);
    		bin = ch.charCodeAt(0).toString(2);
    		while(bin.length<16) bin = "0" + bin;
    		binText += bin;
    		}
    	return binText;
    	}
    
    function $ToUnicode(binText) {
    	var text = "", bin, dec;
    	if (binText.length%16!=0) return false;
    	for(var i=0, n=binText.length/16; i<n; ++i) {
    		bin = binText.substr(16*i, 16);
    		dec = 0;
    		for(var j=0; j<16; ++j) dec += Math.pow(2, j) * bin.charAt(15-j);
    		text += String.fromCharCode(dec);
    		}
    	return text;
    	}
    I'm not sure why your version converts "\n" to "00000000" instead of "00001010", so I took it out. But if you really need it to be like that, add the following to $ToBinary's for loop right after you assign (ch):
    Code:
    if(ch=="\n") {
    	binText += "00000000";
    	continue;
    	}
    and this to $ToAscii's outer for loop right after you assign (bin):
    Code:
    if(bin=="00000000") {
    	text += "\n";
    	continue;
    	}
    Last edited by Trinithis; 07-17-2007 at 09:26 PM.
    Trinithis

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    By "full Unicode" I presume you mean UTF-16. Remember, UTF-8 and UTF-32 exist as well.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Yep, that's what I mean, but if I'm not mistaken, JS uses UTF-16 anyway
    Trinithis

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    js> for(var x in "あ") print("あ".charCodeAt(x).toString(2));
    11100011
    10000001
    10000010
    I believe it uses whatever encoding it's fed. Here we see it using UTF-8, but my console was in UTF-8.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Could you explain how your code works?

    The js> is unfamiliar. (Perhaps a link if need be.)

    As is using the for-in like that. Shouldn't there only be 1 charCodeAt(i) because the string is only 1 character long?
    Trinithis

  7. #17
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think that is simply the line of javascript, with 'js>' just showing you that that line is js, and that the output of that line is written below. I don't think that's a command. If so, I'm just as lost as you are
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #18
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The js> is unfamiliar.
    It's just the Spidermonkey command-line interpreter. You type code, it gives results. No great mystery.
    As is using the for-in like that.
    Don't do this normally, it's not at all portable. Javascript allows (but JScript doesn't allow) us to treat strings as arrays and loop through or access individual characters as we would elements of an array.
    Shouldn't there only be 1 charCodeAt(i) because the string is only 1 character long?
    It's looping through all three bytes of the character. In UTF-16, that character is only two bytes long; in UTF-8, it's three (thus, UTF-16 is preferred for content with lots of multibyte characters).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #19
    Join Date
    Aug 2006
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why 00000000 for \n

    I didn't know the binary code for \n so I just use one that was open.

  10. #20
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    function $ToBinary(text){
    this.tobin="";this.context=text;
    for (this.i=0;this.i<this.context.length;this.i++){
    this.let=this.context.substr(this.i,1);
    if (this.let=="\n"){this.tobin=this.tobin+'00000000'}
    if (this.let=="A"){this.tobin=this.tobin+'01000001'}
    if (this.let=="B"){this.tobin=this.tobin+'01000010'}
    if (this.let=="C"){this.tobin=this.tobin+'01000011'}
    if (this.let=="D"){this.tobin=this.tobin+'01000100'}
    if (this.let=="E"){this.tobin=this.tobin+'01000101'}
    if (this.let=="F"){this.tobin=this.tobin+'01000110'}
    if (this.let=="G"){this.tobin=this.tobin+'01000111'}
    if (this.let=="H"){this.tobin=this.tobin+'01001000'}
    if (this.let=="I"){this.tobin=this.tobin+'01001001'}
    if (this.let=="J"){this.tobin=this.tobin+'01001010'}
    if (this.let=="K"){this.tobin=this.tobin+'01001011'}
    if (this.let=="L"){this.tobin=this.tobin+'01001100'}
    if (this.let=="M"){this.tobin=this.tobin+'01001101'}
    if (this.let=="N"){this.tobin=this.tobin+'01001110'}
    if (this.let=="O"){this.tobin=this.tobin+'01001111'}
    if (this.let=="P"){this.tobin=this.tobin+'01010000'}
    if (this.let=="Q"){this.tobin=this.tobin+'01010001'}
    if (this.let=="R"){this.tobin=this.tobin+'01010010'}
    if (this.let=="S"){this.tobin=this.tobin+'01010011'}
    if (this.let=="T"){this.tobin=this.tobin+'01010100'}
    if (this.let=="U"){this.tobin=this.tobin+'01010101'}
    if (this.let=="V"){this.tobin=this.tobin+'01010110'}
    if (this.let=="W"){this.tobin=this.tobin+'01010111'}
    if (this.let=="X"){this.tobin=this.tobin+'01011000'}
    if (this.let=="Y"){this.tobin=this.tobin+'01011001'}
    if (this.let=="Z"){this.tobin=this.tobin+'01011010'}
    if (this.let=="a"){this.tobin=this.tobin+'01100001'}
    if (this.let=="b"){this.tobin=this.tobin+'01100010'}
    if (this.let=="c"){this.tobin=this.tobin+'01100011'}
    if (this.let=="d"){this.tobin=this.tobin+'01100100'}
    if (this.let=="e"){this.tobin=this.tobin+'01100101'}
    if (this.let=="f"){this.tobin=this.tobin+'01100110'}
    if (this.let=="g"){this.tobin=this.tobin+'01100111'}
    if (this.let=="h"){this.tobin=this.tobin+'01101000'}
    if (this.let=="i"){this.tobin=this.tobin+'01101001'}
    if (this.let=="j"){this.tobin=this.tobin+'01101010'}
    if (this.let=="k"){this.tobin=this.tobin+'01101011'}
    if (this.let=="l"){this.tobin=this.tobin+'01101100'}
    if (this.let=="m"){this.tobin=this.tobin+'01101101'}
    if (this.let=="n"){this.tobin=this.tobin+'01101110'}
    if (this.let=="o"){this.tobin=this.tobin+'01101111'}
    if (this.let=="p"){this.tobin=this.tobin+'01110000'}
    if (this.let=="q"){this.tobin=this.tobin+'01110001'}
    if (this.let=="r"){this.tobin=this.tobin+'01110010'}
    if (this.let=="s"){this.tobin=this.tobin+'01110011'}
    if (this.let=="t"){this.tobin=this.tobin+'01110100'}
    if (this.let=="u"){this.tobin=this.tobin+'01110101'}
    if (this.let=="v"){this.tobin=this.tobin+'01110110'}
    if (this.let=="w"){this.tobin=this.tobin+'01110111'}
    if (this.let=="x"){this.tobin=this.tobin+'01111000'}
    if (this.let=="y"){this.tobin=this.tobin+'01111001'}
    if (this.let=="z"){this.tobin=this.tobin+'01111010'}
    if (this.let==" "){this.tobin=this.tobin+'00100000'}
    if (this.let=="0"){this.tobin=this.tobin+'00110000'}
    if (this.let=="1"){this.tobin=this.tobin+'00110001'}
    if (this.let=="2"){this.tobin=this.tobin+'00110010'}
    if (this.let=="3"){this.tobin=this.tobin+'00110011'}
    if (this.let=="4"){this.tobin=this.tobin+'00110100'}
    if (this.let=="5"){this.tobin=this.tobin+'00110101'}
    if (this.let=="6"){this.tobin=this.tobin+'00110110'}
    if (this.let=="7"){this.tobin=this.tobin+'00110111'}
    if (this.let=="8"){this.tobin=this.tobin+'00111000'}
    if (this.let=="9"){this.tobin=this.tobin+'00111001'}
    if (this.let=="!"){this.tobin=this.tobin+'00100001'}
    if (this.let=="\""){this.tobin=this.tobin+'00100010'}
    if (this.let=="#"){this.tobin=this.tobin+'00100011'}
    if (this.let=="$"){this.tobin=this.tobin+'00100100'}
    if (this.let=="&#37;"){this.tobin=this.tobin+'00100101'}
    if (this.let=="&"){this.tobin=this.tobin+'00100110'}
    if (this.let=="'"){this.tobin=this.tobin+'00100111'}
    if (this.let=="("){this.tobin=this.tobin+'00101000'}
    if (this.let==")"){this.tobin=this.tobin+'00101001'}
    if (this.let=="*"){this.tobin=this.tobin+'00101010'}
    if (this.let=="+"){this.tobin=this.tobin+'00101011'}
    if (this.let==","){this.tobin=this.tobin+'00101100'}
    if (this.let=="-"){this.tobin=this.tobin+'00101101'}
    if (this.let=="."){this.tobin=this.tobin+'00101110'}
    if (this.let=="/"){this.tobin=this.tobin+'00101111'}
    if (this.let==":"){this.tobin=this.tobin+'00111010'}
    if (this.let==";"){this.tobin=this.tobin+'00111011'}
    if (this.let=="<"){this.tobin=this.tobin+'00111100'}
    if (this.let=="="){this.tobin=this.tobin+'00111101'}
    if (this.let==">"){this.tobin=this.tobin+'00111110'}
    if (this.let=="?"){this.tobin=this.tobin+'00111111'}
    if (this.let=="@"){this.tobin=this.tobin+'01000000'}
    if (this.let=="["){this.tobin=this.tobin+'01011011'}
    if (this.let=="\\"){this.tobin=this.tobin+'01011100'}
    if (this.let=="]"){this.tobin=this.tobin+'01011101'}
    if (this.let=="^"){this.tobin=this.tobin+'01011110'}
    if (this.let=="_"){this.tobin=this.tobin+'01011111'}
    if (this.let=="`"){this.tobin=this.tobin+'01100000'}
    if (this.let=="{"){this.tobin=this.tobin+'01111011'}
    if (this.let=="|"){this.tobin=this.tobin+'01111100'}
    if (this.let=="}"){this.tobin=this.tobin+'01111101'}
    if (this.let=="~"){this.tobin=this.tobin+'01111110'}
    if (this.let=="�"){this.tobin=this.tobin+'10000000'}
    if (this.let=="�"){this.tobin=this.tobin+'10100001'}
    if (this.let=="�"){this.tobin=this.tobin+'10100010'}
    if (this.let=="�"){this.tobin=this.tobin+'10100011'}
    if (this.let=="�"){this.tobin=this.tobin+'10100100'}
    if (this.let=="�"){this.tobin=this.tobin+'10100101'}
    if (this.let=="�"){this.tobin=this.tobin+'10100110'}
    if (this.let=="�"){this.tobin=this.tobin+'10100111'}
    /* There's more... this is actually too long to post! */
    }
    return this.tobin;
    }
    Dear gods! You do realise that you can convert a single-byte character to binary by doing:
    Code:
    this.let.charAt(0).toString(2);
    ... didn't you? And what's with the this.x, this.y? this in this case is window, since the functions weren't called as properties of anything. Those are unnecessarily globals, and the this. bit is redundant.
    Code:
    function frontPad(s, len, chr) {
      for(var r = s.toString(); r.length < len; r = (chr || "0") + r);
      return r;
    }
    
    function group(arr, len) {
      for(var i = 0, r = [], n = arr.length; i < n; ++i) {
        if(i % len === 0)
          r.push([]);
        r[r.length - 1].push(arr[i]);
      }
      return r;
    }
    
    function StrToBin(str, charsiz) { // I refuse to use $.
      for(var i = -1, r = "", n = str.length - 1; i < n; r += frontPad(str.charCodeAt(++i).toString(2), charsiz || 8));
      return r;
    }
    
    function BinToStr(bin, charsiz) {
      for(var i = 0, cs = group(bin.split(""), charsiz || 8), n = cs.length; i < n; cs[i] = String.fromCharCode(parseInt(cs[i++].join(""), 2)));
      return cs.join("");
    }
    Last edited by Twey; 07-18-2007 at 12:55 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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