「文字コードの変換」の編集履歴(バックアップ)一覧に戻る

文字コードの変換 - (2010/01/08 (金) 14:57:44) の編集履歴(バックアップ)


  • WinAPIを使うべき処理
ShiftJIS→Unicode(UCS-2)変換
MultiByteToWideChar関数を使う。
MultiByteToWideChar(外部ページ) に詳しい。誰かコピーして。

Unicode(UCS-2)→ShiftJIS変換
WideCharToMultiByte関数を使う。
WideCharToMultiByte(外部ページ) に詳しい。誰かコピーして。

  • WinAPIを使わなくてもプログラム側で簡単に変換できるもの
ShiftJIS→EUC-JP変換
ShiftJISとEUC-JPはエンコードが違うだけで同じ符号体系を用いている。その為、この変換は比較的容易い。

Sub SJIS2EUCJP(src As *Byte,dst As *Byte)
    Dim is=0 As Long,id=0 As Long
    Do
        If src[is]=0 then
            dst[id]=0
            Exit Do
        Else If src[is]<&H80 then
            dst[id]=src[is]
            is++
            id++
        Else If (&HA1<=src[is]) And (src[is]<=&HDF) then
            dst[id]=&H8E
            dst[id+1]=src[is]
            is++
            id+=2
        Else
            Dim a As Long,b As Long
            b=src[is+1]-&H3F
            If b=0 then
                dst[id]=0
                Exit Do
            End If
            If b>63 then b--
            a=src[is]*2-&H101
            If a>62 then a-=128
            If b>94 then
                b-=94
                a++
            End If
            If (a<1) Or (94<a) Or (b<1) Or (94<b) then
                dst[id]=0
                Exit Do
            End If
            dst[id]=a+&HA0
            dst[id+1]=b+&HA0
            is+=2
            id+=2
        End If
    Loop
End Sub

EUC-JP→ShiftJIS変換
Sub EUCJP2SJIS(src As *Byte,dst As *Byte)
    Dim is=0 As Long,id=0 As Long
    Do
        If src[is]=0 then
            dst[id]=0
            Exit Do
        Else If src[is]<&H80 then
            dst[id]=src[is]
            is++
            id++
        Else If src[is]=&H8E then
            dst[id]=src[is+1]
            is+=2
            id++
        Else If src[is]=&H8F then'JIS X 0212-1990文字集合だが、Shift-JISには無いため?で代用する。
            dst[id]=Asc("?")
            is+=3
            id++
        Else
            Dim a As Long,b As Long
            a=src[is]-&HA0
            b=src[is+1]-&HA0
            If (a<1) Or (94<a) Or (b<1) Or (94<b) then
                dst[id]=0
                Exit Do
            End If
            a--
            If a and 1 then
                a--
                b+=94
            End If
            a=(a>>1)+&H81
            If b>63 then b++
            If a>&H9F then a+=64
            dst[id]=a
            dst[id+1]=b+&H3F
            is+=2
            id+=2
        End If
    Loop
End Sub

Unicode(UCS-2)→Unicode(UTF-8)変換
UCS-2とUTF-8はエンコードが違うだけで同じ符号体系を用いている。その為、この変換は比較的容易い。
Sub UCS22UTF8(src As *Byte,dst As *Byte)
    Dim is=2 As Long,id=0 As Long
    Dim fBE As Long
    If (src[0]=&HFF) and (src[1]=&HFE) then'BOM
        fBE=0
    Else If (src[0]=&HFE) and (src[1]=&HFF) then
        fBE=1
    Else'default=Big Endian(RFC 2781)
        fBE=1
        is-=2
    End If
    Do
        If src[is]=0 And src[is+1]=0 then
            dst[id]=0
            Exit Do
        Else
            Dim c As Long
            c=src[is+(1-fBE)] As Long*256+src[is+fBE]
            If c<&H80 then
                dst[id]=c
                id++
            Else If c<&H800 then
                dst[id]=&HC0 Or (c>>6)
                dst[id+1]=&H80 Or (c and &H3F)
                id+=2
            Else If c<&H10000 then
                dst[id]=&HE0 Or (c>>12)
                dst[id+1]=&H80 Or ((c>>6) and &H3F)
                dst[id+2]=&H80 Or (c and &H3F)
                id+=3
            Else
                dst[id]=0
                Exit Do
            End If
            is+=2
        End If
    Loop
End Sub

以下編集中...
Unicode(UTF-8)→Unicode(UCS-2)