4 Replies Latest reply: Apr 29, 2009 2:07 AM by 699554 RSS

    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...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: Convert hexadecimal data to Integer format
          807588
          switch(ch) {
            case '0':  //your code here
            case '1':  //your code here
               //more cases here
          
            case 'F':  //your code here
            default:  //your code here
          }
          Each digit must be multiplied by 16^n where n runs from 0 to maxDigits you are using.
          • 2. Re: Convert hexadecimal data to Integer format
            699554
            I am not quite sure what format your hexadecimal string is in, it would be nice to see some examples. If the format is "0x..", then simply remove the "0x" prefix and call the valueOf() method below with radix 16.
            Integer.valueOf("hexString", 16);
            Mel
            • 3. Re: Convert hexadecimal data to Integer format
              699554
              morgalr wrote:
              switch(ch) {
              case '0':  //your code here
              case '1':  //your code here
              //more cases here
              
              case 'F':  //your code here
              default:  //your code here
              }
              Each digit must be multiplied by 16^n where n runs from 0 to maxDigits you are using.
              I wonder if you could simplify it with a method.
              public byte foo(String s){  //requires work
                return Integer.valueOf(s, 16).byteValue();
              }
              Mel
              • 4. Re: Convert hexadecimal data to Integer format
                807588
                Yes, very easily, but that is not what the OP asked for, may times this type of question is in response to a homework problem and they have to work the problem the specific way it was asked. Teachers will do this to emphasize and clarify what the student knows or may not know about a number system.