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.
mattcknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:

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

String shiftJIS = new String( theUTF8String.getBytes("Shift_JIS") );
0
mattcknightAuthor Commented:
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
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

TimYatesCommented:
You should print your exception...

          } catch (Exception e){
                      //return e;
               e.printStackTrace() ;
               return "0";
               }
0
petmagdyCommented:
what is ur JDK version? if JDK 1.4.2 make sure that the jar ( {urJDKHome/jre/lib/charsets.jar) is in ur classpath
0
mattcknightAuthor Commented:
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
0
petmagdyCommented:
>> 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
0
petmagdyCommented:
or better:
java -cp .;C:\j2sdk1.4.2_06\jre\lib;%CLASSPATH% strConvert
0
mattcknightAuthor Commented:
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.
0
petmagdyCommented:
ok append to ur classpath c:\j2sdk1.4.2_06\lib;c:\j2sdk1.4.2_06\jre\lib
0
mattcknightAuthor Commented:
still getting "java.io.UnsupportedEncodingException: SJIS" in the thread output....?
0
petmagdyCommented:
could u please run this command and get the us the result:


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

did I do something wrong when I edited the CLASSPATH environment varaiable in windows?
0
petmagdyCommented:
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
0
mattcknightAuthor Commented:
There is no User variable for CLASSPATH - and only one in SYSTEM.  That is the one i (first created) and edited.
0
petmagdyCommented:
can u post me the Enviroment variable u set, the enviroment name and value
0
mattcknightAuthor Commented:
OK, changed ColdFusion to not use a specific JVM - the program is no longer throwing an exception.
0
mattcknightAuthor Commented:
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?

0
petmagdyCommented:
    public String exec(String value, String targetEncoding){
          try{
              return new String( theUTF8String.getBytes(targetEncoding) );
          }catch (Exception e){
               //return e;
               e.printStackTrace() ;
               return "0";
               }
     }

and call it like this:
String encoded = exec("Test String", "Shift_JIS");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattcknightAuthor Commented:
actually - "return new String( value.getBytes(targetEncoding) );"

Thank you very much - that worked!
0
petmagdyCommented:
welcome :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.