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

Java - Hex2Text - First Draft


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

Posted by Cpt SAJChurchey On 2005-02-24 20:07:17
In Reply to Desktop Version Posted by CinC snarkles On 2005-02-15 17:19:31

Cpt
Cpt SAJChurchey


I had to write a separate conversion for hex -> decimal so that I could get the ascii value. I'm wondering whether or not I should do the same thing for the Bin2Text function.
public static String hex2Text(String str){
          if(str == null || str.trim().length() == 0)
            return "";
          str = str.trim();
                  str = str.toLowerCase();  //ensures all input is lower case
          if(str.matches("[^0-9a-f\\s]")){
                /*
                  User needs to be warned that their input is invalid
                  Method must terminate
                */
                return "";  //Until proper exception can be written
          }
          StringTokenizer tokenizedStr = new StringTokenizer(str);
          StringBuffer text = new StringBuffer();
          while(tokenizedStr.hasMoreTokens()){
                String hexString = tokenizedStr.nextToken();
                int bytelen = hexString.length();
                if(bytelen > 4){
                 //User needs to be told input is not valid
                 return ""; //Until exception can be written.
                }
                int  decimal = hex2decimal(hexString);
                int intValue = 0;
                for(int i = 0;i < strlen;++i){
                  int oneOrZero = decimal % 10;
                  decimal /= 10;
                  intValue += Math.pow(2,i) * oneOrZero;
                }//end for
                char textEquivalent = (char) intValue;
                text.append(textEquivalent);
          }//end while
          return text.toString();
        }

        private static int hex2decimal(String hex){
          int decimal = 0;
          for(int i = hex.length() - 1;i >= 0;--i){
                int asciiValue = hex.charAt(i);
                if(asciiValue >= '0' && asciiValue <= '9')
                  asciiValue -= 48;
                else{
                  asciiValue -= 87;
            }
                decimal += Math.pow(16,i) * asciiValue;
          }//end for
          return decimal;
        }//end hex2decimal()
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.02407 seconds


About Us | Privacy Policy | Mission Statement | Help