| Author |
How do I print extended ascii characters in Java ?
|
Vinay Singh
Ranch Hand
Joined: Dec 15, 2004
Posts: 174
|
|
Hi,
We have a old application in asp-sqlserver .users create login n change password from there.
Now i m working on new modules Grails(/java) and same old database. while trying to change password we use following logic for encryption(new & old website change pwd).
But if i use this & save in DB ,it never matches becoz in java after ASCII value(127) it prints everything as ? .
how can i print the extended ascii values while encryption.?
Code is as below-
public class StrSearch1{
public static void main(String args[]){
String encoded = encode("sviadha");
System.out.println("******Encoded*******"+ encoded);
}
/**
* This would encrypt the password
* @param encode - string to be encoded
* @return
*/
static String getEncodedString(String encode) {
StringBuffer outputBuff = new StringBuffer()
print("ENCODE START --input string=="+encode)
for (int i = 0; i < encode.length(); ++i) {
char ch = encode.charAt(i)
int asciiCh =(int) ch
int encryptCh = asciiCh + 20
outputBuff = outputBuff.append((char)encryptCh)
}
print("END ENCODE--output string=="+outputBuff)
return outputBuff.toString()
}
}
Result -
******Encoded******* ??ux|u
NOte :- it returns ? for any char after 'k'
Tried byte arrays as well.Please suggest.
|
Technical quiz and interview questions SCJP 6 mock practice test
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
You aren't producing "extended ASCII" characters there. There's no such thing in Java. You're just producing Unicode characters, and probably they don't represent printable characters. You could check the code charts at the Unicode site.
But if they are unprintable characters, so what? Why would you need to print the encoded password anyway?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
Because, please not becoz: and not n. Please look at this FAQ.
And where are you printing? Windows command prompts are notorious for only supporting a restricted range of characters.
|
 |
Vinay Singh
Ranch Hand
Joined: Dec 15, 2004
Posts: 174
|
|
Cambell , I will take care not to use shortcuts next time.
Paul I do not have to print it. I am printing it because I am testing it . And I am using eclipse on Windows.
I need the extended ASCII because that is how it would be stored in database.
The table would be used by old version of application which need to read this password from the same location and they would not be using java to decrypt it.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Ah, that explains the not-very-strong encryption method then.
You're actually getting the same characters, it's just that Windows calls them "Extended ASCII". This is a very loose term, several dozen incompatiable character sets have been invented over the years which claim to be "extended ASCII". That's why even Microsoft is trying to get people to use Unicode these days, and Java just uses Unicode.
Anyway if you write the same things to the database that the old ASP application would have written, you should be okay. The best way to do that would be to convert the string to bytes, add the 20 to each of the bytes, and store the resulting byte array in the database. Otherwise you are going to get mixed up with string encoding, and that's likely to convert the unprintable characters to question marks. Which isn't what you want. So just stick with the bytes.
|
 |
Vinay Singh
Ranch Hand
Joined: Dec 15, 2004
Posts: 174
|
|
Thanks a lot, Paul.
Now we are handling it on database side .Passing the Password string to Stored procedure and there encrypting it.
- Vinay
|
 |
Vinay Singh
Ranch Hand
Joined: Dec 15, 2004
Posts: 174
|
|
As you mentioned, Java just uses Unicode.
what about other languages like asp, c, c++?
Thanks
Vinay
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
C uses "char"s which are unsigned 8-bit integers with values from 0 to ff(=255) inclusive.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
But C99 (the C standard from 1999) also has the type wchar_t, which is for wide characters. Annoyingly, and as is typical in C, a "wide character" may mean a Unicode character, but it may also be only 8 bits wide. There's nothing in C that's guaranteed to work as a Unicode character with all C compilers.
Windows has had support for Unicode strings since about Windows 95 or 98.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Vinay Singh
Ranch Hand
Joined: Dec 15, 2004
Posts: 174
|
|
Thanks for "C" related information.
Java uses the "platform default charset" which can be retrieved via Charset.defaultCharset() as the default value for various encoding-related methods.
Say, i am using windows then my java code will support charset of Operating system.
Can we add new Charset so that java support it?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
Java already supports several dozen charsets. And yes, I believe you can create your own charsets. That's not relevant to your original question, though.
|
 |
 |
|
|
subject: How do I print extended ascii characters in Java ?
|
|
|