CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

--regex


[Replies] [Reply] [View by Thread] [Help]
[Back To SNEAK Development Forum]

Posted by Cpt SAJChurchey On 2005-02-22 22:28:15
In Reply to One thing . . . Posted by Cpt SAJChurchey On 2005-02-22 03:17:07

Cpt
Cpt SAJChurchey


public static String string2Bin(String str){
        str = str.trim();  //Gets rid of leading/trailing whitespace
        int strlen = str.length();
        if(strlen == 0) //Matches NULL and all white space
          return "";    //Short-circuits function
        StringBuffer binaryString = new StringBuffer((strlen * 9));
        /*
          Assuming all UTF-8 input.  The capacity of the buffer
          needs to be strlen * 9.  This cuts down on what can
          be a time consuming dynamic resizing on large input
          sets.  Should only resize once at most, and only if
          the input contains a UTF-16 character.
        */
        for(int i=0;i < strlen;++i){ //Iterates through string
          int decimal = str.charAt(i);  //Gets "ASCII value"
          String binary = Integer.toBinaryString(decimal);
          if(decimal <= 255){
                /*
                  This case tests for ASCII, Extended ASCII and UTF-8
                  integer values.  It completes the octet for these
                  8-bit encodings by padding with leading zeros.
                */
                int trailingZeros = 8 - binary.length();
                for(int j = 0; j < trailingZeros;++j)
                  binaryString.append("0");
          }//end if
          else{ //UTF-16
                /*
                  This case completes the 16-bit encoding of UTF-16
                  characters by padding with leading zeros
                */
                int trailingZeros = 16 - binary.length();
                for(int k = 0;k < trailingZeros;++k)
                  binaryString.append("0");
          }//end else
          binaryString.append(binary);
                //Adds binary representation to generated leading zeros.
      binaryString.append(" ");
   }//end for
   return binaryString.toString();
}//end string2Bin()
Cpt SAJChurchey
C/O of
Editorial
OSI Staff edit0r
OSI Feedback Representative



Replies:


Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.08479 seconds


About Us | Privacy Policy | Mission Statement | Help