Haha.
I'll be wondering what's keeping him busy til he arrives![]()
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
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%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:
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: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; }
and this to $ToAscii's outer for loop right after you assign (bin):Code:if(ch=="\n") { binText += "00000000"; continue; }
Code:if(bin=="00000000") { text += "\n"; continue; }
Last edited by Trinithis; 07-17-2007 at 09:26 PM.
Trinithis
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!
Yep, that's what I mean, but if I'm not mistaken, JS uses UTF-16 anyway
Trinithis
I believe it uses whatever encoding it's fed. Here we see it using UTF-8, but my console was in UTF-8.Code:js> for(var x in "あ") print("あ".charCodeAt(x).toString(2)); 11100011 10000001 10000010
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!
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
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
It's just the Spidermonkey command-line interpreter. You type code, it gives results. No great mystery.The js> is unfamiliar.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.As is using the for-in like that.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).Shouldn't there only be 1 charCodeAt(i) because the string is only 1 character long?
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!
I didn't know the binary code for \n so I just use one that was open.
Dear gods! You do realise that you can convert a single-byte character to binary by doing: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=="%"){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;
}... 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:this.let.charAt(0).toString(2);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