SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java-ASCII Hex to binary conversion

    Hi,

    I have a 4 bytes string in ASCII Hex, eg) 32:30:30:34, I need to convert into 2 bytes of Hex string to 2004 and further convert it become binary 10000000000100. can anyone show me that how to achieve it ?

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have below code the convert the four bytes of ASCII hex to 2 bytes of Hex.

    Code:
     public static String convertHexToString(String hex){
    		  
    		  StringBuilder sb = new StringBuilder();
    		  StringBuilder temp = new StringBuilder();
    	 
    		  for( int i=0; i<hex.length()-1; i+=2 ){
    	 
    		      String output = hex.substring(i, (i + 2));
    	
    		      int decimal = Integer.parseInt(output, 16);
    		     
    		      sb.append((char)decimal);
    	 
    		      temp.append(decimal);
    		  }
    		  System.out.println("Decimal : " + temp.toString());
    	 
    		  return sb.toString();
    	  }

    However, the binary value is not correct when i convert using method
    Integer.toBinaryString(Integer.parseInt(hex))

    The binary value given is 11111010100 instead of 10000000000100

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    problem solved..need to put radix=16


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •