Link to home
Start Free TrialLog in
Avatar of mattcknight
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.
Avatar of petmagdy
petmagdy
Flag of Canada image


String shiftJIS = theUTF8String.getBytes("Shift_JIS");
sorry a little correction

String shiftJIS = new String( theUTF8String.getBytes("Shift_JIS") );
Avatar of mattcknight
mattcknight

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
Avatar of TimYates
You should print your exception...

          } 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/charsets.jar) is in ur classpath
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.UnsupportedEncodingException: SJIS"

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
or better:
java -cp .;C:\j2sdk1.4.2_06\jre\lib;%CLASSPATH% strConvert
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.
ok append to ur classpath c:\j2sdk1.4.2_06\lib;c:\j2sdk1.4.2_06\jre\lib
still getting "java.io.UnsupportedEncodingException: SJIS" in the thread output....?
could u please run this command and get the us the result:


echo %CLASSPATH%
result is: C:\Java\j2re1.4.2_05\lib

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
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
OK, changed ColdFusion to not use a specific JVM - the program is no longer throwing an exception.
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?

ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually - "return new String( value.getBytes(targetEncoding) );"

Thank you very much - that worked!
welcome :-)