VB.NET では LenB LeftB MidB RightB といった文字列をバイトで操作する関数がなくなってしまいました
VB6.0 までは StrConv 関数と併用して処理することで対応できましたが VB.NET では関数自体がなくなっています
特に VB6.0 で作成されたプログラムを VB.NET にコンバートする場合に これらの関数の代用関数を予め用意しておけば 開発を
効率よく進めることができるでしょう
'' st_Value (必須) 対象文字列 Public Function LenB(ByVal st_Value As String) As Integer '' 文字列のバイト数を取得 Return System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(st_Value) End Function '' st_Value (必須) 対象文字列 '' i_Length (必須) 指定バイト数 Public Function LeftB(ByVal st_Value As String, ByVal i_Length As Integer) As String Dim i_ByteCount As Integer = 0 '' 文字列のバイト数 Dim i_ByteLength As Integer = i_Length '' 取得バイト数 Dim b_Value() As Byte '' バイト配列の文字列 '' 長さ0の文字列の場合 If (st_Value.Length = &H0) Then Return "" End If '' 文字列のバイト数を取得 i_ByteCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(st_Value) '' 取得バイト数が文字列バイト数より大きい場合、文字列バイト数までを取得する If (i_ByteCount < i_ByteLength) Then i_ByteLength = i_ByteCount End If '' 文字列をシフトJISのバイト配列に変換 b_Value = System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(st_Value) '' 指定されたバイト数をシフトJIS文字列に変換 Return System.Text.Encoding.GetEncoding("Shift_JIS").GetChars(b_Value, 0, i_ByteLength) End Function '' st_Value (必須) 対象文字列 '' i_Length (必須) 指定バイト数 Public Function RightB(ByVal st_Value As String, ByVal i_Length As Integer) As String Dim i_ByteCount As Integer = 0 '' 文字列のバイト数 Dim i_ByteStart As Integer = 0 '' バイト開始位置 Dim i_ByteLength As Integer = i_Length '' 取得バイト数 Dim b_Value() As Byte '' バイト配列の文字列 '' 長さ0の文字列の場合 If (st_Value.Length = &H0) Then Return "" End If '' 文字列のバイト数を取得 i_ByteCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(st_Value) '' 取得バイト数が文字列バイト数より大きい場合、文字列バイト数までを取得する If (i_ByteCount < i_ByteLength) Then i_ByteLength = i_ByteCount Else '' バイト開始位置を文字列バイト数−取得バイト数とする i_ByteStart = i_ByteCount - i_ByteLength End If '' 文字列をシフトJISのバイト配列に変換 b_Value = System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(st_Value) '' 指定されたバイト数をシフトJIS文字列に変換 Return System.Text.Encoding.GetEncoding _ ("Shift_JIS").GetChars(b_Value, i_ByteStart, i_ByteLength) End Function '' st_Value (必須) 対象文字列 '' i_Start (必須) 開始位置 '' i_Length (省略可) 指定バイト数 Public Function MidB(ByVal st_Value As String, ByVal i_Start As Integer, _ Optional ByVal i_Length As Integer = Nothing) As String Dim i_ByteCount As Integer = 0 '' 文字列のバイト数 Dim i_ByteStart As Integer = i_Start - 1 '' バイト開始位置(0開始のため-1) Dim i_ByteLength As Integer = i_Length '' 取得バイト数 Dim b_Value() As Byte '' バイト配列の文字列 '' 長さ0の文字列の場合 If (st_Value.Length = &H0) Then Return "" End If '' 文字列のバイト数を取得 i_ByteCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(st_Value) '' 指定バイト数が省略された場合 If (IsNothing(i_Length) = True) Then '' 開始位置から最終バイトまでを取得するよう取得バイト数を計算 i_ByteLength = i_ByteCount - i_ByteStart End If '' 開始位置がバイト数より大きい場合 If (i_ByteCount < i_Start) Then Return "" End If '' 開始位置+取得バイト数が文字列バイト数より大きい場合、文字列バイト数までを取得する If (i_ByteCount < i_Length + i_ByteStart) Then i_ByteLength = i_ByteCount - i_ByteStart End If '' 文字列をシフトJISのバイト配列に変換 b_Value = System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(st_Value) '' 指定されたバイト数をシフトJIS文字列に変換 Return System.Text.Encoding.GetEncoding _ ("Shift_JIS").GetChars(b_Value, i_ByteStart, i_ByteLength) End Function