?
Solved

I want a small java program to convert a UTF-8 string to SHIFT-JIS

Posted on 2004-11-15
21
Medium Priority
?
2,910 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:mattcknight
  • 11
  • 9
21 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585358

String shiftJIS = theUTF8String.getBytes("Shift_JIS");
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585369
sorry a little correction

String shiftJIS = new String( theUTF8String.getBytes("Shift_JIS") );
0
 

Author Comment

by:mattcknight
ID: 12585407
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 12585424
You should print your exception...

          } catch (Exception e){
                      //return e;
               e.printStackTrace() ;
               return "0";
               }
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585452
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
 

Author Comment

by:mattcknight
ID: 12585519
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585567
>> 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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585588
or better:
java -cp .;C:\j2sdk1.4.2_06\jre\lib;%CLASSPATH% strConvert
0
 

Author Comment

by:mattcknight
ID: 12585613
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585650
ok append to ur classpath c:\j2sdk1.4.2_06\lib;c:\j2sdk1.4.2_06\jre\lib
0
 

Author Comment

by:mattcknight
ID: 12585678
still getting "java.io.UnsupportedEncodingException: SJIS" in the thread output....?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585688
could u please run this command and get the us the result:


echo %CLASSPATH%
0
 

Author Comment

by:mattcknight
ID: 12585718
result is: C:\Java\j2re1.4.2_05\lib

did I do something wrong when I edited the CLASSPATH environment varaiable in windows?
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585736
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
 

Author Comment

by:mattcknight
ID: 12585771
There is no User variable for CLASSPATH - and only one in SYSTEM.  That is the one i (first created) and edited.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585804
can u post me the Enviroment variable u set, the enviroment name and value
0
 

Author Comment

by:mattcknight
ID: 12585806
OK, changed ColdFusion to not use a specific JVM - the program is no longer throwing an exception.
0
 

Author Comment

by:mattcknight
ID: 12585817
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
 
LVL 13

Accepted Solution

by:
petmagdy earned 200 total points
ID: 12585862
    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
 

Author Comment

by:mattcknight
ID: 12585915
actually - "return new String( value.getBytes(targetEncoding) );"

Thank you very much - that worked!
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12585929
welcome :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question