Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By JosAH

Thread: Converting Hex to ASCII

  1. #1
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Converting Hex to ASCII

    Hi All,

    I try to use below codes to convert Hex String "1111737999630745" to ASCII.

    When I read the result in Notepad++ with HEX-Editor plugin, I find attached image which is different from above String, there is one additional c2 and I am quite confused.

    Please help me to resolve this issue.Converting Hex to ASCII-ascii.png

    Java Code:
    public static void main(String[] args) throws IOException {
    		Test strToHex = new Test();
    		 
    		File file = new File("D:/filename");
    		 
    		// if file doesnt exists, then create it
    		if (!file.exists()) {
    			file.createNewFile();
    		}
    
    		FileWriter fw = new FileWriter(file.getAbsoluteFile());
    		BufferedWriter bw = new BufferedWriter(fw);
    		bw.write(strToHex.convertHexToString("1111737999630745"));
    		bw.close();
    
    	}
    	
    	public String convertHexToString(String hex){
    		 
    		  StringBuilder sb = new StringBuilder();
    		  StringBuilder temp = new StringBuilder();
    	 
    		  //49204c6f7665204a617661 split into two characters 49, 20, 4c...
    		  for( int i=0; i<hex.length(); i+=2 ){
    	 
    		      //grab the hex in pairs
    		      String output = hex.substring(i, (i + 2));
    		      System.out.println("Decimal 1: " + output);
    		      //convert hex to decimal
    		      int decimal = Integer.parseInt(output, 16);
    		      System.out.println("Decimal : " + decimal);
    		      //convert the decimal to character
    		      sb.append((char)decimal);
    	 
    		      temp.append(decimal);
    		  }
    		  System.out.println("Decimal : " + temp.toString());
    	 
    		  return sb.toString();
    	  }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    Hex String "1111737999630745" to ASCII.
    What is the ASCII String you expect to get from that String of hex characters?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    You refer to the image. I just want to know why there is a "c2" inside the String. My original String do not have this char.
    Quote Originally Posted by Norm View Post
    What is the ASCII String you expect to get from that String of hex characters?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    You refer to the image
    No, I was referring to the text of the post.

    Given this String: "1111737999630745" what String do you want to convert it to?

    Or if you don't like that String, post another String of hex digits and the String that you want to convert it to.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    I would like to convert this Hex String "1111737999630745" to ASCII.

    The code shows how I do the convert and I saved it into a file. When I opened the file in Notepad++ with HEX-Editor plugin, I find attached image which is different from above String. There is an additional c2.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    I would like to convert this Hex String "1111737999630745" to ASCII.
    What do you want the results to be?
    Can you post the String that you want get from the conversion of that String?

    I have no idea where the 0xc2 came from. There must be other code that put it there.
    Last edited by Norm; 05-16-2014 at 08:18 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    The output String has been highlighted in blue, it is not readable.
    I opened the file in Notepad++ with HEX-Editor plugin (View in Hex), and the c2 highlighted in red should not be there.
    Converting Hex to ASCII-untitled.png

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    The methods you are using to write Strings uses a charset to decode the chars in the String to bytes. Not all 256 char values can be converted.

    Don't write Strings. Write bytes to preserve their contents.
    Last edited by Norm; 05-16-2014 at 08:37 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    Thanks.

    Could you show some sample code to use bytes? The whole String "1111737999630745" need to be converted to bytes?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    Change your code to cast the int value in decimal to byte instead of char
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    Is it like this?
    But the results look more weird after conversion:
    Converting Hex to ASCII-qq-20140517121802.png

    Java Code:
    public String convertHexToString(String hex){
    		 
    		  StringBuilder sb = new StringBuilder();
    		  StringBuilder temp = new StringBuilder();
    	 
    		  //49204c6f7665204a617661 split into two characters 49, 20, 4c...
    		  for( int i=0; i<hex.length(); i+=2 ){
    	 
    		      //grab the hex in pairs
    		      String output = hex.substring(i, (i + 2));
    		      System.out.println("Decimal 1: " + output);
    		      //convert hex to decimal
    		      int decimal = Integer.parseInt(output, 16);
    		      System.out.println("Decimal : " + decimal);
    		      //convert the decimal to character
    		      sb.append((byte)decimal);
    	 
    		      temp.append(decimal);
    		  }
    		  System.out.println("Decimal : " + temp.toString());
    	 
    		  return sb.toString();
    	  }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    Forget about using a String. Put the converted hex digits into a byte array and write the contents of that array to the file.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    Quote Originally Posted by Norm View Post
    Forget about using a String. Put the converted hex digits into a byte array and write the contents of that array to the file.
    Could you please modify my code? I do not how to figure out.

    Thanks.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    Use a byte[] instead of StringBuilder to hold the bytes as they are converted.
    Change what the method returns to a byte[]
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Converting Hex to ASCII

    You have been bitten by the unknown character encoding, used by FileWriters; while your StringBuffers holds the characters 0x11, 0x11, 0x73, 0x79, 0x99, 0x63, 0x07 and 0x45, the FileWriter converts those characters to their encoded byte values and you don't know what their default encoding rule is, that's why you're surprised by the result. Better write bytes (not chars) with the above values and use a FileOutputStream instead.

    kind regards,

    Jos
    jim829 likes this.
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Converting Hex to ASCII

    Well, I just converted it and I got everything except the C2. So either it was introduced into the file somehow or the plugin is displaying the hex incorrectly.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    Have you changed my code?
    It is so weird.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Converting Hex to ASCII

    What is weird?
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    windieccf is offline Member
    Join Date
    May 2014
    Posts
    9
    Rep Power
    0

    Default Re: Converting Hex to ASCII

    I got the C2 but u donot.

    Have you changed any code?

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Converting Hex to ASCII

    Did you read Jos' comment? #15

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2012, 05:32 AM
  2. Using ASCII...
    By putrify in forum New To Java
    Replies: 4
    Last Post: 09-10-2011, 06:17 AM
  3. help - Ascii to hex and hex to ascii conversion
    By rjagan in forum New To Java
    Replies: 8
    Last Post: 06-14-2011, 01:53 PM
  4. Converting ASCII format
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 02-17-2009, 04:55 PM
  5. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 05:43 AM

Posting Permissions

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