Sunday, December 11, 2011

Rube Goldberg Programming

So I was peer reviewing some code to create a BASE64 string from binary input (in PL1 where we can't just use someone's library.) This code takes a array of bytes to convert, and for each 3 bytes creates a 4 character BASE64 representation. The conversion of the 3 bytes is an impressive Rube Goldberg bit of coding. It first takes each input byte and converts it into an array of binary 1's and 0's. It then takes the binary array and converts it into a character array of 1's and 0's, while breaking the 3 bytes into a 4 sets of 6 1's and 0's. It takes each of the 4 character arrays and converts them into a character Hexadecimal representation. It then converts each of the Hexadecimal representations into a single BASE64 character. The conversion from BASE64 back to binary went through the same steps in reverse.

The conversion of the 3 bytes into 4 BASE64 characters can be done in a single loop. You overlay the 3 bytes with an array of 24 bits. Then for each of output character, you loop over the bits the character is going to represent, building the BASE64 value. You then convert the value into the BASE64 character. This is about 10 lines of code to do the encode and another 10 for the decode. This person took over 100 to do the encoding and another 100 for the decoding.

Gaaaaaaaaa!

0 Comments:

Post a Comment

<< Home