Results 1 to 6 of 6
Thread: Urgent Unicode/Ascii Problem
- 03-22-2010, 12:25 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
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:
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: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)); } } } }
But those boxes are without any fill when displayed in eclipse.Java Code:no mttr wr you o tr you r
If anyone needs more information, please tell me.
- 03-22-2010, 02:38 PM #2
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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:
But you can configure it to use UTF-8 by defaultJava Code:/*The character encoding for the default locale*/ String fileEncoding = System.getProperty("file.encoding"); //Cp1252 on my Windows
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:
So you will end up with your favorite encoding and be sure to findJava Code:// setting the default encoding programmatically System.setProperty( "file.encoding", "UTF-8" );
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?
- 03-22-2010, 07:26 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
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:
Which, when decrypted looks like this:Java Code:nopqrstuvwxyz{|}~
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.Java Code:abcdefghijklmnopq
Last edited by HackerOfDoom; 03-23-2010 at 12:36 AM. Reason: Tested further and realized it didn't work
- 03-23-2010, 12:46 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 21
- Rep Power
- 0
Sorry, but, bump. I need help with this, to get the regular printable and extended (128-) ascii encoding.
- 03-23-2010, 04:20 PM #5
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
So if you set global encoding
in eclipse.ini file
with:
Now all workspaces and all projects in them will use it.Java Code:-Dfile.encoding=UTF-8
One more thing to check: -eclipse console encoding.
Is it still :Java Code:Run => Run Configurations => Common tab => Console Encoding radio button
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:
You see...decoding is done well.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)); } ....
- 03-23-2010, 04:26 PM #6
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
-
Problem with writing unicode characters in a file
By ze snow in forum New To JavaReplies: 1Last Post: 02-23-2010, 10:47 PM -
Unicode string serach problem
By saurabh01 in forum Advanced JavaReplies: 2Last Post: 07-02-2009, 10:22 AM -
Fedora Itext Unicode Problem
By gautamn in forum Java 2DReplies: 0Last Post: 04-13-2009, 08:12 AM -
Reading ASCII / Unicode character in Java
By NGRaju in forum New To JavaReplies: 2Last Post: 09-16-2008, 10:02 PM -
Unicode problem
By rovshanb in forum JDBCReplies: 0Last Post: 02-14-2008, 06:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks