convert ?code

hi folks i've a meesage in my email

iam trying to made rightClick->encode->arabic but it seems as it is

iam trying to make a java program to take this message 阙?and convert it to arabic

publicstaticvoid main(String[] args)throws IOException{

Scanner sc=new Scanner(System.in,"UTF-8");

byte b[]=sc.nextLine().getBytes();

ByteArrayInputStream a=new ByteArrayInputStream(b);

StringBuffer out=new StringBuffer();

for(int n;(n=a.read(b))!=-1;)

out.append(new String(b,0,n));

System.out.println(out);

}

still there is no result , iam change the charset to window-1256

the problem stay

any suggestions !!

[1145 byte] By [eaajea] at [2007-10-3 10:38:41]
# 1

Never use getBytes() or the String(byte[]) constructors.

Even the versions that let you specify the encoding are almost certainly the wrong tools for the job. Trying to do charset conversions within your program only makes the task much more difficult than it needs to be. Just use the correct encoding when you read the data into your program and when you write it out.

uncle_alicea at 2007-7-15 6:02:42 > top of Java-index,Java Essentials,New To Java...
# 2
so what can i dothis the only way i find it
eaajea at 2007-7-15 6:02:42 > top of Java-index,Java Essentials,New To Java...
# 3
oops , there is no body attemp to convert ?to other language !!by the way, what is the name of this language ?
eaajea at 2007-7-15 6:02:42 > top of Java-index,Java Essentials,New To Java...
# 4

I'm not sure what you're trying to do, so I'll just try to explain what you are doing.

Scanner sc=new Scanner(System.in,"UTF-8");

This Scanner will read the input from the console using UTF-8 instead of the system default encoding. You would only do this if you knew the input was not using the encoding that Java thinks is the system default (I suppose that could happen if you were piping the output from another program into this one). On a Windows machine in an Arabic locale, I would expect the system default to be windows-1256.

byte b[]=sc.nextLine().getBytes();

After converting the input to a String (probably incorrectly), this line converts it back to byte array. Garbage in, garbage out.

ByteArrayInputStream a=new ByteArrayInputStream(b);

StringBuffer out=new StringBuffer();

for(int n;(n=a.read(b))!=-1;)

out.append(new String(b,0,n));

This is where it really gets weird: an InputStream is created from the byte array, which reads the bytes right back into the array they came out of. Since the output array is exactly the same size as the input array (being the same array), the loop only goes through one pass. Then the bytes are converted to a String using the system default encoding. I would have expected the program to throw exceptions at several points, but it doesn't. It just happily pumps out garbage.

You might find this link helpful; someone posted it in another thread:

http://www.java2s.com/ExampleCode/Development-Class/ConvertEncoding.htm

uncle_alicea at 2007-7-15 6:02:42 > top of Java-index,Java Essentials,New To Java...