Results 1 to 2 of 2

Thread: Converting vbFromUnicode from vba to vb.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2018
    Posts
    18

    Converting vbFromUnicode from vba to vb.net

    I have a program that was created in vba, and I am working on attempting to convert it to work in vb.net. There are two functions that use vbUniCode and vbFromUnicode in a strConv.

    The two functions are:

    Code:
        Private Function CommRead(Handle As Integer, DataBuffer As String, Size As Integer) As Integer
            Dim bytesRead As Integer
            Dim status As Integer
            Dim buffer() As Byte
    
            ReDim buffer(Size)
    
            'Read from port
            status = Relay_Functions.ReadFile(Handle, buffer(0), Size, bytesRead, 0)
    
            If (status = 0) Then
                CommRead = 0
            Else
                'Convert the array back in to string
                DataBuffer = Left(StrConv(buffer, vbUnicode), Size)
                CommRead = bytesRead
            End If
    
        End Function
    
        Private Function CommWrite(Handle As Integer, Data As String) As Integer
            Dim bytesWritten As Integer
            Dim status As Integer
            Dim buffer() As Byte
    
            ReDim buffer(Len(Data))
            buffer = StrConv(Data, vbFromUnicode)
    
            'Write data to port
            status = Relay_Functions.WriteFile(Handle, buffer(0), Len(Data), bytesWritten, 0)
    
            If (status = 0) Then
                CommWrite = 0
            Else
                CommWrite = bytesWritten
            End If
    
        End Function
    Been trying to find an answer, but haven't been able to find anything sold that works in what the correct conversion is.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Converting vbFromUnicode from vba to vb.net

    In VB.NET, you use the Encoding class and those derived from it to convert between text and binary data. If you have a Byte array and you want to convert it to text based on a specific encoding then you call GetString on the appropriate Encoding object, e.g. Encoding.Unicode.GetString. The GetBytes method does the inverse. You should do a bit of reading and/or experimentation with the available options as it's not necessarily UnicodeEncoding that you need. It might be UTF8Encoding or something else. I've rarely done this sort of thing so I'm not sure.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width