Results 1 to 6 of 6
  1. #1
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default Urgent Unicode/Ascii Problem

    Hello everyone, I was just testing my program this morning when I realized that some of the characters that I modified the ascii value of were not displaying correctly in the console window. My code is as follows:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Crypto {
    	public static void main(String[] args) throws InterruptedException {
    		System.out.println("Enter 'e' for encryption or 'd' for decryption:");
    	
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		char choice = 0;
    		try {
    			choice = (char) br.read();
    		} catch (IOException e) {
    			System.out.println("Error!");
    			System.exit(1);
    		}
    
    		if (choice == 'e')
    		{
    		System.out.println("Enter the phrase to be encrypted:");
    		
    		BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    		String phrase = null;
    		try {
    			phrase = br1.readLine();
    		} catch (IOException e) {
    			System.out.println("Error!");
    			System.exit(1);
    		}
    		System.out.println("Now enter a number between 1 and 100:");
    
    		BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
    		String shift = null;
    		try {
    			shift = br2.readLine();
    		} catch (IOException e) {
    			System.out.println("Error!");
    			System.exit(1);
    		}
    		
    		
    		int counter;
    		System.out.print("\nReallocating memory to binary");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(2000);
    		System.out.print("\nTriangulating the dynamic reflex");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(2000);
    		System.out.print("\nModifiying the rotating cleartext");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		Thread.sleep(1000);
    		System.out.print(".");
    		System.out.print("\n");
    		Thread.sleep(2000);
    		
    		System.out.println("The encrypted text is:");
    		for (counter = 0; counter < phrase.length(); counter++)	
    		{
    			int p = counter;
    			int shiftNum = Integer.parseInt(shift.trim());
    		System.out.print((char)(phrase.charAt(p)+ shiftNum));
    		}
    		Thread.sleep(1000);
    		System.out.println("\nPlease save this encryption key: " + shift);
    			}
    		
    		
    		
    		else if (choice == 'd')
    		{
    			 System.out.println("Enter the encrypted text:");
    
    				BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
    				String phraseBack = null;
    				try {
    					phraseBack = br3.readLine();
    				} catch (IOException e) {
    					System.out.println("Error!");
    					System.exit(1);
    				}
    				System.out.println("Now enter the encryption key:");
    				BufferedReader br4 = new BufferedReader(new InputStreamReader(System.in));
    				String key = null;
    				try {
    					key = br4.readLine();
    				} catch (IOException e) {
    					System.out.println("Error!");
    					System.exit(1);
    				}
    				System.out.print("\nCalibrating the byte loss");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(2000);
    				System.out.print("\nSimulating the interprocessor cooling");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(2000);
    				System.out.print("\nRendering the final manipulation");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				Thread.sleep(1000);
    				System.out.print(".");
    				System.out.print("\n");
    				Thread.sleep(2000);
    			
    				int counterBack;
    				for (counterBack = 0; counterBack < phraseBack.length(); counterBack++)	
    				{
    					int x = phraseBack.length();
    					int pBack = counterBack;
    					int finalKey = Integer.parseInt(key.trim());
    					
    					System.out.print((char)(phraseBack.charAt(pBack) - finalKey));
    				}
    					
    		}
    
    	}
    }
    Please pay no attention to the (text...), that was just to make it look really complicated. Like the Spore text. Anyway, My exact problem is that the output of my program when the first choice = "e" is flawed, some ascii or unicode characters (I don't know where it switches) are just displaying as question marks. For instance, if I type in 'e', then for the phrase 'no matter where you go, there you are', then for the encryption key '55', when I try to decrypt it again it gives me:

    Java Code:
    no mttr wr you o tr you r
    But those boxes are without any fill when displayed in eclipse.
    If anyone needs more information, please tell me.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    You are working there with The extended ASCII codes (character code 128-255).
    ASCII extensions have many variants.
    There are several different variations of the 8-bit ASCII table.

    What OS are you using?
    Your JVM in using the local character set of your OS.
    Your Eclipse uses it for writing source files.

    Print this to see what are you using:
    Java Code:
    /*The character encoding for the default locale*/
     String fileEncoding = System.getProperty("file.encoding"); //Cp1252 on my Windows
    But you can configure it to use UTF-8 by default
    Here is link with very simple instructions:

    Martin Ahrer – together we’ll make IT eclipse encoding settings


    If you want to change the default encoding for files you read and write, including the console,
    you need to set the file.encoding system property.

    You can do this programmatically with:
    Java Code:
    // setting the default encoding programmatically
    System.setProperty( "file.encoding", "UTF-8" );
    So you will end up with your favorite encoding and be sure to find
    somewhere this ASCII extensions of yours, so you can test your code
    and see what happens in console.

    And here is a link of one extension:

    ASCII Code - The extended ASCII table
    (see The extended ASCII codes (character code 128-255) )

    are you OK with this?

  3. #3
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Thank you! Took a little bit of understanding to figure out that Eclipse had a "set global encoding" option, but yeah! Works perfectly now.
    Ok I take it back somewhat, It started working with higher ascii values but stopped with some of the lower ones. For instance, when I input 'abcdefghijklmnopqrstuvwxyz', with an encryption key of '13', I get this:
    Java Code:
    nopqrstuvwxyz{|}~
    Which, when decrypted looks like this:
    Java Code:
    abcdefghijklmnopq
    Which obviously is missing a few chars. Can anyone help me? This project is due Thursday, I need to know the exact code for how to import the encoding system.
    Last edited by HackerOfDoom; 03-23-2010 at 12:36 AM. Reason: Tested further and realized it didn't work

  4. #4
    HackerOfDoom is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Sorry, but, bump. I need help with this, to get the regular printable and extended (128-) ascii encoding.

  5. #5
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    So if you set global encoding
    in eclipse.ini file
    with:

    Java Code:
    -Dfile.encoding=UTF-8
    Now all workspaces and all projects in them will use it.

    One more thing to check: -eclipse console encoding.


    Java Code:
     Run => Run Configurations => Common tab => Console Encoding radio button
    Is it still :
    Default inheriter (Cp 1252) ?
    It should be UTF-8.

    Strange thing is number of encoded characters in your example.
    'abcdefghijklmnopqrstlm' (26)
    to
    nopqrstuvwxyz{|}~ (17)

    how come (9) are missing? Or are maybe not printable...???

    Problem is when copy/paste of encoded:
    4nopqrstuvwxyz{|}~€�‚ƒ„…†‡
    and then running decode through console.

    Problem does not exists if you perform decoding after encoding
    in same cycle
    To test this simply add this code:


    Java Code:
    ...
    /**/
    			[B]StringBuilder sb = new StringBuilder();[/B]
    			/**/
    			
    			for (counter = 0; counter < phrase.length(); counter++)	
    			{
    				int p = counter;
    				//System.out.print("p: " + p);
    				int shiftNum = Integer.parseInt(shift.trim()); //55
    				//System.out.print((char)(phrase.charAt(p)+ shiftNum));
    				System.out.print((char)(phrase.charAt(p)+ shiftNum));
    				[B]sb.append((char)(phrase.charAt(p)+ shiftNum));[/B]
    			}
    			Thread.sleep(1000);
    			System.out.println("\nPlease save this encryption key: " + shift);
    			
    
    			/* ADDED */
    
    			[U]/*auto decription*/[/U]
    
    			String phraseBack = sb.toString();
    			int counterBack;
    			for (counterBack = 0; counterBack < phraseBack.length(); counterBack++)	
    			{
    				int x = phraseBack.length();
    				int pBack = counterBack;
    				int finalKey = Integer.parseInt(shift.trim());
    
    				System.out.print((char)(phraseBack.charAt(pBack) - finalKey));
    			}
    ....
    You see...decoding is done well.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Java deals with everything as Unicode, so when you add or subtract from a char (which you have represented as an int, which is fine), then you end up with a different Unicode value. Unicode and ASCII, or whatever character set you are actually using, only align to a certain extent. Basically, I'm saying that your approach won't work.

Similar Threads

  1. Problem with writing unicode characters in a file
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 10:47 PM
  2. Unicode string serach problem
    By saurabh01 in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2009, 10:22 AM
  3. Fedora Itext Unicode Problem
    By gautamn in forum Java 2D
    Replies: 0
    Last Post: 04-13-2009, 08:12 AM
  4. Reading ASCII / Unicode character in Java
    By NGRaju in forum New To Java
    Replies: 2
    Last Post: 09-16-2008, 10:02 PM
  5. Unicode problem
    By rovshanb in forum JDBC
    Replies: 0
    Last Post: 02-14-2008, 06:41 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
  •