osx - How to translate Text to Binary with Cocoa? -


i'm making simple cocoa program can encode text binary , decode text. tried make script , not close accomplishing this. can me? has include 2 textboxes , 2 buttons or whatever best, thanks!

there 2 parts this.

the first encode characters of string bytes. sending string datausingencoding: message. encoding choose determine bytes gives each character. start nsutf8stringencoding, , experiment other encodings, such nsunicodestringencoding, once working.

the second part convert every bit of every byte either '0' character or '1' character, that, example, letter a, encoded in utf-8 single byte, represented 01000001.

so, converting characters bytes, , converting bytes characters representing bits. these 2 completely separate tasks; second part should work correctly stream of bytes, including valid stream of encoded characters, invalid stream of encoded characters, , indeed isn't text @ all.

the first part easy enough:

- (nsstring *) stringofbitsfromencoding:(nsstringencoding)encoding     ofstring:(nsstring *)inputstring {     //encode characters bytes using utf-8 encoding. bytes contained in nsdata object, receive.     nsdata *data = [string datausingencoding:nsutf8stringencoding];      //i did these 2 separate jobs.     return [self stringofbitsfromdata:data]; } 

for second part, you'll need loop through bytes of data. c for loop job there, , this:

//this method we're using above. i'll leave out method signature , let fill in. - … {     //find out how many bytes data object contains.     nsuinteger length = [data length];     //get pointer bytes. “const” here means promise not change values of of bytes. (the compiler may give warning if don't include this, since we're not allowed change these bytes anyway.)     const char *bytes = [data bytes];      //we'll store output here. there 8 bits per byte, , we'll putting in 1 character per bit, we'll tell nsmutablestring should make room (the number of bytes times 8) characters.     nsmutablestring *outputstring = [nsmutablestring stringwithcapacity:length * 8];      //the loop. start initializing 0, increment (add 1 it) after each pass. keep looping long < length; when >= length, loop ends.     (nsuinteger = 0; < length; ++i) {         char thisbyte = bytes[i];          (nsuinteger bitnum = 0; bitnum < 8; ++bitnum) {             //call function, i'll show definition of in moment, value of bit @ given index within given character.             bool bit = getbitatindex(thisbyte, bitnum);              //if bit 1, append '1' character; if 0, append '0' character.             [outputstring appendformat: @"%c", bit ? '1' : '0'];         }     }      return outputstring; } 

bits 101 (or, 1100101)

bits literally digits in base 2. humans in western world write out numbers in base 10, number number no matter base it's written in, , every character, , every byte, , indeed every bit, number.

digits—including bits—are counted lowest place, according exponent base raised find magnitude of place. want bits, base 2, our place values are:

  • 2^0 = 1: ones place (the lowest bit)
  • 2^1 = 2: twos place (the next higher bit)
  • 2^2 = 4: fours place
  • 2^3 = 8: eights place

and on, 2^7. (note highest exponent 1 lower number of digits we're after; in case, 7 vs. 8.)

if reminds of reading “the ones place”, “the tens place”, “the hundreds place”, etc. when kid, should: it's exact same principle.

so byte such 65, (in utf-8) represents character 'a', sum of:

  2^7 × 0 = 0 + 2^6 × 0 = 64 + 2^5 × 1 = 0 + 2^4 × 0 = 0 + 2^3 × 0 = 0 + 2^2 × 0 = 0 + 2^1 × 0 = 0 + 2^0 × 1 = 1 = 0 + 64 +0+0+0+0+0 + 1 = 64 + 1 = 65 

back when learned base 10 numbers kid, noticed ten “10”, 1 hundred “100”, etc. true in base 2 well: 10^x “1” followed x “0”s in base 10, 2^x “1” followed “x” 0s in base 2. so, example, sixty-four in base 2 “1000000” (count zeroes , compare table above).

we going use these exact-power-of-two numbers test each bit in each input byte.

finding bit

c has pair of “shift” operators insert zeroes or remove digits @ low end of number. former called “shift left”, , written <<, , can guess opposite.

we want shift left. want shift 1 left number of bit we're after. equivalent raising 2 (our base) power of number; example, 1 << 6 = 2^6 = “1000000”.

testing bit

c has operator bit testing, too; it's &, bitwise , operator. (do not confuse &&, logical , operator. && using whole true/false values in making decisions; & 1 of tools working bits within values.)

strictly speaking, & not test single bits; goes through bits of both input values, , returns new value bits bitwise , of each input pair. so, example,

  01100101 & 00101011 ----------   00100001 

each bit in output 1 if , if both of corresponding input bits 1.

putting these 2 things together

we're going use shift left operator give number 1 bit, nth bit, is set—i.e., 2^n—and use bitwise , operator test whether same bit set in our input byte.

//this c function takes char , int, promising not change either one, , returns bool. bool getbitatindex(const char byte, const int bitnum) //it method, this: //- (bool) bitatindex:(const int)bitnum inbyte:(const char)byte //but have change code above. (feel free try both ways.) {     //find 2^bitnum, number 1 bit set. example, when bitnum 6, number “1000000”—a single 1 followed 6 0s—in binary.     const int poweroftwo = 1 << bitnum;      //test whether same bit set in input byte.     bool bitisset = byte & poweroftwo;      return bitisset; } 

a bit of magic should acknowledge

the bitwise , operator not evaluate single bit—it not evaluate 1 or 0. remember above example, in & operator returned 33.

the bool type bit magic: time convert value bool, automatically becomes either 1 or 0. not 0 becomes 1; 0 becomes 0.

the objective-c bool type does not this, why used bool in code above. free use whichever prefer, except should use bool whenever deal expects bool, particularly when overriding methods in subclasses or implementing protocols. can convert , forth freely, though not losslessly (since bool change non-zero values described above).

oh yeah, said text boxes too

when user clicks on button, stringvalue of input field, call stringofbitsfromencoding:ofstring: using reasonable encoding (such utf-8) , string, , set resulting string new stringvalue of output field.

extra credit: add pop-up button user can choose encoding.

extra credit: populate pop-up button of available encodings, without hard-coding or hard-nibbing list.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -