// http://en.wikipedia.org/wiki/UTF-8 function stringToByteArray(a,b,c,d,e,f){ for( b = [ e = f = d = 0 ] // initialise variables ; c = a.charCodeAt(d++) // get the character code from the string ; ){ c < 128 // under 128 is UTF-8 (ASCII range), 1 byte ? b[e] = c // add to byte array : c < 2048 // under 2048 2bytes ? ( b[e] = 192 + (c >> 6), // shift down 6bits, add 192 to put it in the right range f = 1 // number of bytes left to process ) : c < 2^16 // under 65536 is 3bytes ? ( b[e] = 224 + (c >> 12), // shift down 12bits and put it into the correct range f = 2 // number of bytes left to process ) : c < 2^21 && ( // finally, under 2097152 is 4bytes b[e] = 240 + (c >> 18), // shift down 18bits and 240 so its in the right range f = 3 // 3bytes left to process ); for( // process the remaining bytes indicated by `f` e++ // move onto the next slot in the byte array ; f-- // -1 and check if greater than 0 still ; ) b[e++] = 128 + (c >> f*6 & 63) // shift f * 6 bits, mask 1byte and add 128 } return b // return the byte array }