|
WideCharToMultiByteとMultiByteToWideCharによる文字コード変換のサンプル(VB.NET)

|
<このサンプルの概要>
WideCharToMultiByteとMultiByteToWideCharを使用しSJIS、JIS、EUC、UTF-7、UTF-8 からVB6内部文字コードである
UNICODE(UTF-16)に変換します。また、UNICODEからの逆変換もします。全ての文字コードをUNICODEに変換出来て、
その逆変換も出来れば全ての文字コード間の変換が UNICODEを通して出来る事になります。UNICODEを通す事による
処理速度等は気にしていません。
VB6でのWideCharToMultiByteとMultiByteToWideCharによる文字コード変換は、意外と簡単でしたが、VB.NETでは
Win32Apiの宣言(Declare)の方法が難しく思考錯誤しました。WideCharToMultiByteとMultiByteToWideCharによる
文字コード変換で残念なのがEUCの変換が出来なかった事です。それ以外の変換ならこれで充分でしょう。
Win32Apiを呼び出しているため .NETのアンマネージ・コードになるのでノータッチ・デプロイメントやClickOnceで
実行する為にはセキュリティを「完全な信頼」に設定する必要があります。
VB6版のサンプルソースもあります。
WideCharToMultiByteとMultiByteToWideCharによる文字コード変換のサンプル(VB6)
関連情報
ADODB.Streamによる文字コード変換のサンプル(VB6/VB.NET)
文字コード判定のサンプル(VB6/VB.NET)
文字コード判定&変換ツール.NET
文字コード判定/変換ツール(VB6のソース付)
★標準モジュール(Module1.vb)
Module Module1
'*************************************************************************
' 機能名 : Module1.vb
' 機能説明 : WideCharToMultiByteとMultiByteToWideCharによる文字コード変換
'*************************************************************************
Declare Function WideCharToMultiByte Lib "kernel32" _
(ByVal CodePage As Integer, _
ByVal dwFlags As Integer, _
ByVal lpWideCharStr As Byte(), _
ByVal cchWideChar As Integer, _
ByVal lpMultiByteStr As Byte(), _
ByVal cchMultiByte As Integer, _
ByVal lpDefaultChar As String, _
ByVal lpUsedDefaultChar As Integer) As Integer
Declare Function MultiByteToWideChar Lib "kernel32" _
(ByVal CodePage As Integer, _
ByVal dwFlags As Integer, _
ByVal lpMultiByteStr As Byte(), _
ByVal cchMultiByte As Integer, _
ByVal lpWideCharStr As Byte(), _
ByVal cchWideChar As Integer) As Integer
'---Shift-JIS関係
' 関数名 : WCMB_Encode_SJIS
' 返り値 : SJIS文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをSJISに変換する
' 備考 : WideCharToMultiByteによる文字コード変換
Public Function WCMB_Encode_SJIS(ByRef strUni As String) As Byte()
WCMB_Encode_SJIS = WCMB_Encode(932, strUni)
End Function
' 関数名 : WCMB_Decode_SJIS
' 返り値 : UNICODE文字列
' 引き数 : bytIn : SJIS文字データ
' 機能説明 : SJIS文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
Public Function WCMB_Decode_SJIS(ByRef bytIn() As Byte) As String
WCMB_Decode_SJIS = WCMB_Decode(932, bytIn)
End Function
'---JIS関係
' 関数名 : WCMB_Encode_JIS
' 返り値 : JIS文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをJISに変換する
' 備考 : WideCharToMultiByteによる文字コード変換
' 備考 : 半角カタカナ有り
Public Function WCMB_Encode_JIS(ByRef strUni As String) As Byte()
WCMB_Encode_JIS = WCMB_Encode(50221, strUni)
End Function
' 関数名 : WCMB_Decode_JIS
' 返り値 : UNICODE文字列
' 引き数 : bytIn : JIS文字データ
' 機能説明 : JIS文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
' 備考 : 半角カタカナ有り
Public Function WCMB_Decode_JIS(ByRef bytIn() As Byte) As String
WCMB_Decode_JIS = WCMB_Decode(50221, bytIn)
End Function
' 関数名 : WCMB_Encode_JIS2
' 返り値 : JIS文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをJISに変換する
' 備考 : WideCharToMultiByteによる文字コード変換
' 備考 : 半角カタカナ無し
Public Function WCMB_Encode_JIS2(ByRef strUni As String) As Byte()
WCMB_Encode_JIS2 = WCMB_Encode(50220, strUni)
End Function
' 関数名 : WCMB_Decode_JIS2
' 返り値 : UNICODE文字列
' 引き数 : bytIn : JIS文字データ
' 機能説明 : JIS文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
' 備考 : 半角カタカナ無し
Public Function WCMB_Decode_JIS2(ByRef bytIn() As Byte) As String
WCMB_Decode_JIS2 = WCMB_Decode(50220, bytIn)
End Function
'---EUC関係
' 関数名 : WCMB_Encode_EUC
' 返り値 : EUC文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをEUCに変換する
' 備考 : WideCharToMultiByteによる文字コード変換
Public Function WCMB_Encode_EUC(ByRef strUni As String) As Byte()
'WCMB_Encode_EUC = WCMB_Encode(51932, strUni)
MsgBox("EUCはサポートされていません", vbCritical)
End Function
' 関数名 : WCMB_Decode_EUC
' 返り値 : UNICODE文字列
' 引き数 : bytIn : EUC文字データ
' 機能説明 : EUC文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
Public Function WCMB_Decode_EUC(ByRef bytIn() As Byte) As String
'WCMB_Decode_EUC = WCMB_Decode(51932, bytIn)
MsgBox("EUCはサポートされていません", vbCritical)
End Function
'---UTF-7関係
' 関数名 : WCMB_Encode_UTF7
' 返り値 : UTF7文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをUTF7に変換する
' 備考 : WideCharToMultiByteによる文字コード変換
Public Function WCMB_Encode_UTF7(ByRef strUni As String) As Byte()
WCMB_Encode_UTF7 = WCMB_Encode(65000, strUni)
End Function
' 関数名 : WCMB_Decode_UTF7
' 返り値 : UNICODE文字列
' 引き数 : bytIn : UTF7文字データ
' 機能説明 : UTF7文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
Public Function WCMB_Decode_UTF7(ByRef bytIn() As Byte) As String
WCMB_Decode_UTF7 = WCMB_Decode(65000, bytIn)
End Function
'---UTF-8関係
' 関数名 : WCMB_Encode_UTF8
' 返り値 : UTF8文字データ
' 引き数 : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データをUTF8に変換する
' 備考 : WideCharToMultiByteによる文字コード変換
Public Function WCMB_Encode_UTF8(ByRef strUni As String) As Byte()
WCMB_Encode_UTF8 = WCMB_Encode(65001, strUni)
End Function
' 関数名 : WCMB_Decode_UTF8
' 返り値 : UNICODE文字列
' 引き数 : bytIn : UTF8文字データ
' 機能説明 : UTF8文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
Public Function WCMB_Decode_UTF8(ByRef bytIn() As Byte) As String
WCMB_Decode_UTF8 = WCMB_Decode(65001, bytIn)
End Function
'---共通関数
' 関数名 : WCMB_Encode
' 返り値 : 出力文字データ
' 引き数 : cp : 出力文字データのコードページ番号
' : strUni: UNICODE文字データ
' 機能説明 : UNICODE文字データを指定のコードに変換する
' 備考 : WideCharToMultiByteによる文字コード変換
Private Function WCMB_Encode(ByVal cp As Integer, ByRef strUni As String) As Byte()
Dim lngInSize As Integer
Dim lngOutSize As Integer
Dim lngRtn As Integer
Dim bytOut() As Byte
Dim bytIn() As Byte
If Len(strUni) <= 0 Then
Exit Function
End If
bytIn = System.Text.Encoding.Unicode.GetBytes(strUni)
lngInSize = UBound(bytIn) + 1
lngOutSize = lngInSize * 5
ReDim bytOut(lngOutSize - 1)
lngRtn = WideCharToMultiByte _
(cp, 0, bytIn, lngInSize, bytOut, lngOutSize, vbNullString, 0)
If lngRtn Then
ReDim Preserve bytOut(lngRtn - 1)
WCMB_Encode = bytOut
End If
End Function
' 関数名 : WCMB_Decode
' 返り値 : UNICODE文字列
' 引き数 : cp : 入力文字データのコードページ番号
' : bytIn : 入力文字データ
' 機能説明 : 入力文字データをUNICODEに変換する
' 備考 : MultiByteToWideCharによる文字コード変換
Private Function WCMB_Decode(ByVal cp As Integer, ByRef bytIn() As Byte) As String
Dim lngInSize As Integer
Dim lngOutSize As Integer
Dim lngRtn As Integer
Dim bytOut() As Byte
If UBound(bytIn) < 0 Then
Exit Function
End If
lngInSize = UBound(bytIn) + 1
lngOutSize = lngInSize * 5
ReDim bytOut(lngOutSize - 1)
lngRtn = MultiByteToWideChar _
(cp, 0, bytIn, lngInSize, bytOut, lngOutSize)
If lngRtn Then
ReDim Preserve bytOut(lngRtn - 1)
WCMB_Decode = System.Text.Encoding.Unicode.GetString(bytOut)
End If
End Function
End Module
★フォームモジュール(Form1.vb)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytOut() As Byte = WCMB_Encode_UTF8("あいうえお亜意卯絵尾アイウエオ1234512345")
Dim strStr As String = WCMB_Decode_UTF8(bytOut)
MsgBox(strStr)
End Sub
<WideCharToMultiByteとMultiByteToWideCharでよく使用するコードページ(CodePage)について>
MultiByteToWideCharとWideCharToMultiByteを使用して文字コードを変換する際によく使用される
文字コードを以下に記述して置きます。
CodePage|文字コード
−−−−−−−−−−−−−−−−−−−
932 |シフトJIS
50220 |JIS
50221 |JIS(Allow 1 byte Kana)
50222 |JIS(Allow 1 byte Kana)(SO/SI)
51932 |EUC
65000 |UTF-7
65001 |UTF-8
VB.NETのEncoding.GetEncoding(0-65535).WebNameとEncoding.GetEncoding(0-65535).EncodingNameで
使用出来る文字コードの一覧が取得出来るようです。