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

Hopefully, it'll work now . . .


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

Posted by Cpt SAJChurchey On 2005-02-21 20:56:32
In Reply to Small clarifications Posted by CinC snarkles On 2005-02-21 20:25:23

Cpt
Cpt SAJChurchey



What I mean by display error is it should probably throw some kind of "NullStringException" or what have you that causes the UI to prompt them to re-enter their string (the current form says, "Fill in the form, dinglefritz! ;)" for example).
Java won't have a problem printing the NULL string, at least it didn't when I ran the test cases. What will essentially happen is. . . nothing . . .such a request would not do anything b/c the function short-circuits. If you're dynamically changing a text field on a UI, the user won't know anything happened.

We don't want to ignore whitespace altogether (whitespace is important within text strings to separate words, paragraphs, etc.), but we *do* want to ignore trailing/leading whitespace, as in:
"    hello"
and
"hello        "
and also entries that are nothing *but* whitespace:
"                    "
So trim'ing the string prior to checking if it's empty should take care of those problems.
Including the trim and the NULL string case. . .

UTF-8 characters can go all the way up to 255 (8 bits). 127 and below are basic ASCII (7 bits)... 127-255 are extended ASCII (8 bits). Any higher than that (256-65535) and they require 2 bytes (16 bits) to hold the full value. So just change your if statement to check for
decimal <= 255
Btw, what's up with checking for negative decimal value?
I think I made the assumption that chars when cast to ints wound up signed instead of unsigned. Essentialy, it was to see if it was in the range of an 8-bit signed integer.
public static String string2Bin(String str){
public static String string2Bin(String str){
        str.trim();
        if(str.matches("\\s+"))
          return "";
        StringBuffer binaryString = new StringBuffer();
        int strlen = str.length();
        for(int i=0;i < strlen;++i){ //faster loop
          int decimal = str.charAt(i);
          String binary = Integer.toBinaryString(decimal);
          if(decimal <= 255){  //UTF-8
                int trailingZeros = 8 - binary.length();
                for(int j = 0; j < trailingZeros;++j)
                  binaryString.append("0");
          }//end if
          else{ //UTF-16
                int trailingZeros = 16 - binary.length();
                for(int k = 0;k < trailingZeros;++k)
                  binaryString.append("0");
          }//end else
          binaryString.append(binary);
      binaryString.append(" ");
   }//end for
   return binaryString.toString();
}//end string2Bin()
I used a regular expression to match all whitespace and NULL cases.
I think it is either just giving the wrong answer for the first character of the Foreign Language case for whatever reason. Or there is something wrong w/ that particular test case. Can you double check it and get back to me?

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.02370 seconds


About Us | Privacy Policy | Mission Statement | Help