Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

VB6 Bytearray.

shamun.tohashamun.toha Member Posts: 4
How to do this in VB6? Any idea or possibilities?

in java code, TCP / ip i can send st byte array:
------------------------------------------------
byte st[]=new byte[256];
st[0]=1;
st[1]=0x11;
st[2]=0x00;
st[3]=0x09;
System.out.println(st);

Comments

  • pVbpVb Member Posts: 4
    : How to do this in VB6? Any idea or possibilities?
    :
    : in java code, TCP / ip i can send st byte array:
    : ------------------------------------------------
    : byte st[]=new byte[256];
    : st[0]=1;
    : st[1]=0x11;
    : st[2]=0x00;
    : st[3]=0x09;
    : System.out.println(st);
    :
    :

    Hi,
    try that:

    Imports System
    Imports Microsoft.VisualBasic

    Module HexTest
    Dim hexDigits As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c, _
    "5"c, "6"c, "7"c, "8"c, "9"c, _
    "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}

    Function ToHexString(ByVal bytes() As Byte) As String
    Dim chars(bytes.Length * 2) As Char
    Dim i As Integer

    For i = 0 To bytes.Length - 1
    Dim b As Integer = bytes(i)
    chars((i * 2)) = hexDigits(b >> 4)
    chars((i * 2) + 1) = hexDigits(b And &HF)
    Next i
    Return New String(chars)
    End Function

    Sub Main()
    Dim b As Byte() = {&H0, &H12, &H34, &H56, &HAA, &H55, &HFF}
    Console.WriteLine(ToHexString(b))
    End Sub
    End Module

    '
    'This code example produces the following results:
    '
    '00123456AA55FF
    '
    By the way,
    I developed a Client for Advantys-T
  • shamun.tohashamun.toha Member Posts: 4
    Thanks, but how to send 256 bytes as header and body, where ASCIIZ format padded by 24 bytes?

    '// Consider 256 byte
    Dim bit() as Byte

    '// To send byte over TCP 0 to 4
    '// Header
    bit(0) = 1
    bit(1) = &H23
    bit(2) = &H6C
    bit(3) = &H1
    bit(4) = &H2

    '// To send byte over TCP 25 byte
    '// Body
    bit(5) = !!!!!!

  • pVbpVb Member Posts: 4
    : Thanks, but how to send 256 bytes as header and body, where ASCIIZ
    : format padded by 24 bytes?
    :
    : '// Consider 256 byte
    : Dim bit() as Byte
    :
    : '// To send byte over TCP 0 to 4
    : '// Header
    : bit(0) = 1
    : bit(1) = &H23
    : bit(2) = &H6C
    : bit(3) = &H1
    : bit(4) = &H2
    :
    : '// To send byte over TCP 25 byte
    : '// Body
    : bit(5) = !!!!!!
    :
    :
    Hi
    I've not tested yet this code but perhaps it can be useful for you:

    Dim canalTab(256) As Byte
    Dim i As Integer
    '****************************
    'padd with null value the canalTab
    canalTab(0) = CByte(&H1)

    For i = 1 To 255

    canalTab(i) = CByte(i Mod 25)

    Next
    canalTab(256) = CByte(0)
    '****************************

    'continue
    canalTab(1) = TestPad(1,"23")
    canalTab(2) = TestPad(2,"6C")
    canalTab(3) = TestPad(3,"H1")
    canalTab(4) = TestPad(4,"H1")
    '..........


    'Test the position in the array
    Function TestPad(ByVal pos as Integer, ByVal hexString as String) as Byte

    If pos Mod 25 =0 Then
    Console.Out.WriteLine("You have an error at " & pos & " position")
    Return CByte(&H1) 'By default
    Else
    Return HexToByte(hexString)
    End if

    End Function

    ' Converts a hexadecimal string to a byte .
    Private Function HexToByte(ByVal hexString As String) As Byte
    Dim returnByte As Byte

    'Test if hexString.Length <>2
    If hexString.Length <>2 Then
    Console.Out.WriteLine _
    ("You have an error for " & hexString & " value")

    Return CByte(&H1) 'By default

    End if

    returnByte = _
    Convert.ToByte(hexString.Substring(0, 2), 16)


    Return returnByte
    End Function 'HexToByte

Sign In or Register to comment.