Statistics

Problem Statement for "EncodeMIME"

Problem Statement

PROBLEM STATEMENT:

Your system has a new requirement to email clients a file.  Your employer, not
surprisingly, is too cheap to buy a third party email library and your team is
assigned to develop an in-house library.  You are assigned to develop the
Multimedia Internet Mail Extensions (MIME) encoding function used to encode the
file attachment.  MIME is used to convert any binary byte into a 'printable'
byte that can be transmitted over the internet (over all types of
hardware/systems) without losing information.  The most popular encoding form
of MIME is Base64 encoding.

The base64 encoding technique takes 3 consecutive characters (8-bits each, most
significant bit first) and encodes them into 4 printable characters (resulting
in a 33% increase in size).  The process is as follows (see Example 1 below):

1) Take the integer value of three characters (from left to right) and convert
them to their binary representation of 8-bits each.
2) Take the three 8-bit sets and concatenate them to form a single set of 24
bits.
3) Break the 24 bit set into four 6 bit sets.
4) Then, for each of the four 6 bit sets, take the integer value of the 6 bit
set and use it as an index into the special base64 table (see below) to find
the resulting encoded character.
5) Repeat until all characters have been processed.

When you reach the end of the string, you may not have enough characters to
create a full 24 bit set (in step 1).  In this case, you should right pad the
set with 0 bits to artificially create the full 24-bit set.  If any 6-bit set
is made up entirely of the padded 0 bit's, that set should be encoded as an
equals sign ("=").  The equal's character, which is not part of the base64
table, lets the decoder know that the character is simply padding and should
not be decoded.

Implement a class EncodeMIME, which contains a method encode.  The method
returns a MIME base64 encoded string from the passed string.

DEFINITION

Class name: EncodeMIME
Method name: encode
Parameters: String
Returns: String

The method signature is:
String encode(String toEncode);
Be sure your method is public.

TopCoder will ensure the validity of the inputs. Inputs are valid if all of the
following criteria are met:
*The toEncode will have a length from 0 to 50, inclusive.
*The toEncode will consist only of letters (a-z,A-Z), numbers (0-9, inclusive),
spaces (' ') and/or the following characters "!@#$%^&*()-_+={},.][/\|<>?" (not
including the double-quotes).

NOTES

*Assume each character in the String is an 8-bit byte.
*The conversion table is:
char[] base64 = 
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'
,'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n'
,'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7'
,'8','9','+','/'}


EXAMPLES

-If the stringToEncode is "WoW" then

               -------------------------------------------------
 The word:     |      W        |        o      |       W       |
               -------------------------------------------------
 Byte value:   |      87       |       111     |       87      |
               -------------------------------------------------
 Bit Pattern:  |0 1 0 1 0 1 1 1 0 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1|
               -------------------------------------------------
 6-Bit Value:  |    21     |    54     |     61    |     23    |
               -------------------------------------------------
 After Lookup: |     V     |     2     |      9    |      X    |
               -------------------------------------------------

 1) Take the integer value of each character. (int)'W'=87 and (int)'o'=111
2) Convert the three integers into their binary representation and concatenate
them together.
 3) Divide the 24-bit set into 4 6-bit sets (010101,110110,111101,010111).
4) Convert each 6-bit binary representation into an integer:  010101=21,
110110=54, 111101=61, 010111=23
5) Use each integer as an index into the table above: base64[21]='V',
base64[54]='2', base64[61]='9', base64[23]='X'.

  so the function returns "V29X".
 
-If the stringToEncode is "TopCoder", then the function returns "VG9wQ29kZXI=".
-If the stringToEncode is "@!#%", then the function returns "QCEjJQ==".
-If the stringToEncode is "", then the function returns "".

Definition

Class:
EncodeMIME
Method:
encode
Parameters:
String
Returns:
String
Method signature:
String encode(String param0)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: