4 Replies Latest reply on Apr 29, 2009 6:22 PM by 807588

    Convert hexadecimal data to Integer format

    807588
      I have a code by which I can converts the bytes read from Socket InputStream to Readable string format.
      boolean inHex = false;
      
                //System.out.println("\n\n***************");
                for (int i = 0; i < bytesRead; i++) {
                     final int value = (buffer[i] & 0xFF);
      
      //               m_out.println("Buffer : "+buffer);
           //          m_out.println("Value : "+value);
                     // If it's ASCII, print it as a char.
                     if (value == '\r' || value == '\n'
                               || (value >= ' ' && value <= '~')) {

                          if (inHex) {
                               stringBuffer.append(']');
                               inHex = false;
                          }
                //          m_out.println("IF : ");
           //               m_out.print((char) value);
                          stringBuffer.append((char) value);
                     } else { // else print the value
                          if (!inHex) {
                               stringBuffer.append('[');
                               inHex = true;
                          }

                          if (value <= 0xf) { // Where's "HexNumberFormatter?"
                               stringBuffer.append("0");
                          }
                          //m_out.println("Value : "+value);
                          //m_out.println("Hex : "+Integer.toHexString(value));
                          stringBuffer.append(Integer.toHexString(value).toUpperCase());
                          
                     }
                }
                
                m_out.println("------ " + connectionDetails.getDescription() + "------");
                m_out.println(stringBuffer);
      After getting the data from above code I manipulate the data and I want to reconvert the data into the actual byte array...
      
      Now the String data contains charaters as well as the Hexadecimal String data..
      
      I am reading the data character by character from stringBuffer (StringBuffer) now how to identify the character that is read from the string as Hexadecimal one or normal charater...