'○VB6 文字列(Byte配列)をInteger配列に格納する

'|   A   |   B   |   C   |   D   | Byte配列(文字列)
'|   A       B   |   C       D   | Integer配列

'Integer の16bitの中に8bitのByte値を2こ並べて配置します

Private Sub Command1_Click()
    Dim pos() As Integer
    pos = Str_toIntegerAr8(Text1.Text)
    
    Debug.Print "--------------"
    Debug.Print Hex(pos(1))
    Debug.Print Hex(pos(2))
    Debug.Print Hex(pos(3))
    Debug.Print Hex(pos(4))
    Debug.Print Hex(pos(5))
    Debug.Print Hex(pos(6))
    Debug.Print Hex(pos(7))
    Debug.Print Hex(pos(8))
    
End Sub


'-------------------------------------------------
'Long型をビットの並びを維持したまたInteger型に変換します
'-------------------------------------------------
Private Function u_CInt(val As Long) As Integer
    If (val > 32767) Then
        u_CInt = CInt(val - 65536)
    Else
        u_CInt = CInt(val)
    End If
End Function

'-------------------------------------------------
'二つのByte型を結合して一つのInteger型を作成
'-------------------------------------------------
Private Function AddByte(b1 As Byte, b2 As Byte) As Integer
    Dim L1, L2 As Long
    L1 = b1
    L2 = b2
    AddByte = u_CInt(L1 * 256 + L2)
End Function

'***************************************************
'文字列をSJISに変換してInteger[8]配列に格納し出力します
'***************************************************
Private Function Str_toIntegerAr8(str As String) As Integer()
    '文字列をS-JIS、バイト配列に変換
    Dim b_ar() As Byte
    b_ar = MidB(StrConv(str, vbFromUnicode), 1, 16)
    
    '配列を複製します
    Dim b_pos(16) As Byte
    Dim i As Integer
    For i = 0 To UBound(b_ar)
        b_pos(i) = b_ar(i)
    Next i

    'ByteをIntegerに変換します
    Dim I_ar(8) As Integer
    I_ar(1) = AddByte(b_pos(0), b_pos(1))
    I_ar(2) = AddByte(b_pos(2), b_pos(3))
    I_ar(3) = AddByte(b_pos(4), b_pos(5))
    I_ar(4) = AddByte(b_pos(6), b_pos(7))
    I_ar(5) = AddByte(b_pos(8), b_pos(9))
    I_ar(6) = AddByte(b_pos(10), b_pos(11))
    I_ar(7) = AddByte(b_pos(12), b_pos(13))
    I_ar(8) = AddByte(b_pos(14), b_pos(15))
    Str_toIntegerAr8 = I_ar
End Function



▲トップページ > Visual BASIC と C#