mattcknight
asked on
I want a small java program to convert a UTF-8 string to SHIFT-JIS
I want a small java program to convert a UTF-8 string to SHIFT-JIS.
I do not want to return the results to System.out, instead - just want a "return jistStr;" at the end of the program.
If possible, i would like to pass in the from encoding and to encoding, as well as the String to convert.
I do not want to return the results to System.out, instead - just want a "return jistStr;" at the end of the program.
If possible, i would like to pass in the from encoding and to encoding, as well as the String to convert.
sorry a little correction
String shiftJIS = new String( theUTF8String.getBytes("Sh ift_JIS") );
String shiftJIS = new String( theUTF8String.getBytes("Sh
ASKER
I was working on the following:
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String sjisStr = new String(str.getBytes("UTF-8 "), "SHIFT_JIS");
return sjisStr;
} catch (Exception e){
//return e;
return "0";
}
}
}
but i always get back 0
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String sjisStr = new String(str.getBytes("UTF-8
return sjisStr;
} catch (Exception e){
//return e;
return "0";
}
}
}
but i always get back 0
You should print your exception...
} catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
} catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
what is ur JDK version? if JDK 1.4.2 make sure that the jar ( {urJDKHome/jre/lib/charset s.jar) is in ur classpath
ASKER
OK, modified my program to:
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String shiftJIS = new String(str.getBytes("Shift _JIS"));
return shiftJIS;
}catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
}
}
and i get "java.io.UnsupportedEncodi ngExceptio n: SJIS"
my JDK is j2sdk1.4.2_06
My CLASSPATH = C:\j2sdk1.4.2_06\bin
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String shiftJIS = new String(str.getBytes("Shift
return shiftJIS;
}catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
}
}
and i get "java.io.UnsupportedEncodi
my JDK is j2sdk1.4.2_06
My CLASSPATH = C:\j2sdk1.4.2_06\bin
>> My CLASSPATH = C:\j2sdk1.4.2_06\bin
I think u mean that this is ur PATH not CLASSPATH, please run ur java application like this :
java -cp %CLASSPATH%;C:\j2sdk1.4.2_ 06\jre\lib strConvert
I think u mean that this is ur PATH not CLASSPATH, please run ur java application like this :
java -cp %CLASSPATH%;C:\j2sdk1.4.2_
or better:
java -cp .;C:\j2sdk1.4.2_06\jre\lib ;%CLASSPAT H% strConvert
java -cp .;C:\j2sdk1.4.2_06\jre\lib
ASKER
OK, maybe I have some other issues here.
I am calling this java program from ColdFusion application server.
I instantiate the java object and call the exec() method. Then output the results of the method call.
Im my windows environment variables, I have a variable names CLASSPATH with a value of C:\j2sdk1.4.2_06\bin
Do I need to make some changes? The program compiles fine, but I get the above java exception when I run the program from ColdFusion.
Cold Fusion is using java.home=C:/Java/j2re1.4. 2_05 - which is also on my windows machine.
I am calling this java program from ColdFusion application server.
I instantiate the java object and call the exec() method. Then output the results of the method call.
Im my windows environment variables, I have a variable names CLASSPATH with a value of C:\j2sdk1.4.2_06\bin
Do I need to make some changes? The program compiles fine, but I get the above java exception when I run the program from ColdFusion.
Cold Fusion is using java.home=C:/Java/j2re1.4.
ok append to ur classpath c:\j2sdk1.4.2_06\lib;c:\j2 sdk1.4.2_0 6\jre\lib
ASKER
still getting "java.io.UnsupportedEncodi ngExceptio n: SJIS" in the thread output....?
could u please run this command and get the us the result:
echo %CLASSPATH%
echo %CLASSPATH%
ASKER
result is: C:\Java\j2re1.4.2_05\lib
did I do something wrong when I edited the CLASSPATH environment varaiable in windows?
did I do something wrong when I edited the CLASSPATH environment varaiable in windows?
make sure that u edit CLASPATH in the System enviroment variables (not user enviroment variable) and their is no CLASSPATH variable in the user enviroment variables that overwrites
ASKER
There is no User variable for CLASSPATH - and only one in SYSTEM. That is the one i (first created) and edited.
can u post me the Enviroment variable u set, the enviroment name and value
ASKER
OK, changed ColdFusion to not use a specific JVM - the program is no longer throwing an exception.
ASKER
Now - how do i modify this:
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String shiftJIS = new String(str.getBytes("Shift _JIS"));
return shiftJIS;
}catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
}
}
to take an input of type String - the string to be converted from UTF-8 to SHIFT_JIS?
import java.io.*;
import java.lang.*;
public class strConvert{
public String exec(){
try{
String str = new String("`This is a test string`");
String shiftJIS = new String(str.getBytes("Shift
return shiftJIS;
}catch (Exception e){
//return e;
e.printStackTrace() ;
return "0";
}
}
}
to take an input of type String - the string to be converted from UTF-8 to SHIFT_JIS?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
actually - "return new String( value.getBytes(targetEncod ing) );"
Thank you very much - that worked!
Thank you very much - that worked!
welcome :-)
String shiftJIS = theUTF8String.getBytes("Sh