Add tags

Add tags to the following comment

Javaの文字列にはエンコーディングの概念がありません(内部的にはすべてUnicode)。

そこで、「Shift_JISのバイト列をEUC-JPのバイト列に変換する」と解釈しました。

せっかくなので、処理系による違いを知りたいです。

私が試したJava 1.6では、Shift_JISの円記号(0x5C)は内部的にバックスラッシュ(U+005C)になるようですが、これは規格通りではありません。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.*;

public class Chars {
    public static void main(String[] args) {
        try {
            String s="こんにちは\\";
            for(byte b:sjis2eucjp(s.getBytes("SJIS"))) System.out.printf("%x ",b);
            
            file(new File("d:\\sjis.txt"),new File("d:\\eucjp.txt"));
        } catch (Exception e) {e.printStackTrace();}
    }
    
    //Shift_JISのバイト列をEUC-JPのバイト列に変換して返す関数
    static public byte[] sjis2eucjp(byte[] sjis) throws Exception{
        return (new String(sjis,"SJIS")).getBytes("EUC-JP");
    }
    
    //Shift_JISで書かれたファイルをEUC-JPに変換してファイル出力
    static public void file(File sjis,File eucjp) throws Exception{
        //効率を上げたい場合はBufferedReader等でラップする
        Reader in=new InputStreamReader(new FileInputStream(sjis),"SJIS");
        Writer out=new OutputStreamWriter(new FileOutputStream(eucjp),"EUC-JP");
        
        int c;
        while((c=in.read())!=-1) out.write(c);
        
        out.close();
        in.close();
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...