<ここからプログラムをダウンロード>
<applet
code="ShowJpString"
width=800 height=300>
<param name=param value="Parameterからだと割と簡単に使える">
</applet>
/*
Encode: SJIS
$Id: ShowJpString.java,v 1.8 2000-08-14 09:04:11+09 kaiya Exp kaiya $
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class ShowJpString extends Applet implements TextListener, ActionListener{
String fromparam="param";
String fromutf="utf";
String frombyte="byte";
String fromsjis="sjis";
String fromraw="raw sjis string";
String fromfield="input from field";
TextField tf;
public void init(){
setFont(new Font("serif",Font.PLAIN,20));
fromparam=getParameter("param");
fromutf="native2ascii¥u3067¥u4e8b¥u524d¥u306b¥u5909¥u63db¥u3057¥u3068¥u3044¥u305f¥u306e¥u306f¥u7c21¥u5358¥u306b¥u8868¥u793a¥u3067¥u304d¥u307e¥u3059";
frombyte=makefrombyte();
fromsjis=makefromsjis("直に書いてあるデータからの変換,getBytesがうまく動かなかった何故?");
fromraw="そのままだと,こうなります(涙)";
tf=new TextField(40);
tf.addTextListener(this);
tf.addActionListener(this);
add(tf);
}
public void paint(Graphics g){
int y=30;
g.drawString("locale="+Locale.getDefault().toString() , 10, y); y+=20;
y+=10;
g.drawString(fromparam , 10, y); y+=20;
g.drawString(fromutf, 10, y); y+=20;
g.drawString(frombyte, 10, y); y+=20;
g.drawString(fromsjis, 10, y); y+=20;
g.drawString(fromraw, 10, y); y+=20;
g.drawString("Perhaps, just a previous line is invalid.", 10, y); y+=20;
g.drawString(fromfield, 10, y); y+=20;
}
private String makefromsjis(String s){
String r="";
byte[] b=new byte[s.length()];
for(int i=0; i<s.length(); i++){
b[i]=(byte)s.charAt(i);
}
try{
r=new String(b, "SJIS");
//r=new String(s.getBytes("SJIS"), "SJIS");
}catch(Exception e){
r=e.toString(); // "error converting from enbededed string.";
}
return r;
}
private String makefrombyte(){
byte[] bytecode={
(byte)0x62, (byte)0x79, (byte)0x74, (byte)0x65, (byte)0x97,
(byte)0xF1, (byte)0x82, (byte)0xC9, (byte)0x95, (byte)0xCF,
(byte)0x8A, (byte)0xB7, (byte)0x82, (byte)0xB5, (byte)0x82,
(byte)0xC4, (byte)0x82, (byte)0xA8, (byte)0x82, (byte)0xA2,
(byte)0x82, (byte)0xC4, (byte)0x82, (byte)0xE0, (byte)0x8E,
(byte)0x67, (byte)0x82, (byte)0xA6, (byte)0x82, (byte)0xE9,
(byte)0x82, (byte)0xAA, (byte)0x81, (byte)0x43, (byte)0x45,
(byte)0x4E, (byte)0x43, (byte)0x4F, (byte)0x44, (byte)0x45,
(byte)0x8F, (byte)0xEE, (byte)0x95, (byte)0xF1, (byte)0x82,
(byte)0xAA, (byte)0x95, (byte)0x4B, (byte)0x97, (byte)0x76,
(byte)0x28, (byte)0x82, (byte)0xB1, (byte)0x82, (byte)0xB1,
(byte)0x82, (byte)0xC5, (byte)0x82, (byte)0xCD, (byte)0x53,
(byte)0x4A, (byte)0x49, (byte)0x53, (byte)0x29
};
String r;
try{
r=new String(bytecode, "SJIS");
}catch(Exception e){
r="error to convert byte to string.";
}
return r;
}
public void textValueChanged(TextEvent te){
String s= tf.getText();
if(!s.equals("")){
fromfield=s;
repaint();
}
}
public void actionPerformed(ActionEvent ae){
tf.setText("");
repaint();
}
}