Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1147
  • Last Modified:

StrConv equivilent in Visual Basic 2008

In VB 6 I used:
dim ba() as Byte
Dim s As String = a value I get from a Char field in SQL Server
'
ba = StrConv(s, vbFromUnicode)

In Visual Basic 2008 I try:
Dim s as String
Dim ba() as Byte
Dim enc as New System.Text.UTF8Encoding

's is returned from SQL Server
ba = enc.GetBytes(s)

I get a result but when I try to use the result I receive an error. What is the equivilent method for converting a String to a Byte array (Notice the FromUnicode parameter)
thanks,
pat
0
mpdillon
Asked:
mpdillon
  • 7
  • 6
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
The string contains more than one byte value? Are the different values separated by a delimiter, like a comma?

In VB.Net, you can use the various Parse methods (like Byte.Parse or Integer.Parse) to convert a string into the number it represents.
0
 
wdosanjosCommented:
I think you don't need to apply any conversion as all strings in VB.NET are already Unicode.  Please post the code that throws an error and indicate what error message do you get.

0
 
Todd GerbertIT ConsultantCommented:
Hmm, perhaps I misunderstood...

You're saying you have the string "ABC" and you expect to get the values 65, 66 and 67?  That'd be:
Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes("ABC")

Open in new window


If you want the unicode bytes of that string (65, 0, 66, 0, 67 and 0), then:
Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes("ABC")

Open in new window


Or, if you want the unicode bytes in big-endian order (0, 65, 0, 66, 0 and 67) then:
Dim bytes As Byte() = System.Text.Encoding.BigEndianUnicode.GetBytes("ABC")

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
Todd GerbertIT ConsultantCommented:
...in those examples the string literal "ABC" is actually Unicode (as wdosanjos pointed out, everything in .Net is unicode).
0
 
mpdillonAuthor Commented:
tgerbert and wdsanjos,
Thanks for your replies. I iwll attach the whole procedure. I didn't earlier because the part that fails is when the Byte array is loaded into a third party control (TextControl from The Imaging source).

All of the data has been stored in SQL server. All the SQL data is in a TEXT field (my mistake. I had said Char Field earlier). The SQL data was populated by a VB 6 program that I wrote. It read data from this third party tool, applied a StrConv to it and saved it into the database.
Currenty, the smae VB 6 program is reading and writing data to the databae. I am writing a new program that needs to read and write data from SQL into the third party vendors .Net version of the  control.
In VB 6 the data was obtained from the Third Party control with:

ba = .TXTextControl1.SaveToMemory(3, False)
s = StrConv(ba, vbUnicode)
s was then written to SQL.

When it was time to display the data, a SQl query returned the TEXT field into a VB 6 String variable. The string variable was converted into a Byte array and loaded into the Third party control.

ba = StrConv(s, vbFromUnicode)
.TXTextControl1.LoadFromMemory ba, 3, True

Now I am using Visual Basic 2008. I am using the .Net version of the vendors control. I perform the same SQL query on the database and assign the data to a Visual Basic 2008 String type variable.

Dim enc As New System.Text.UTF8Encoding
        ba = enc.GetBytes(s)
        Me.TextControl1.Load(ba, TXTextControl.BinaryStreamType.InternalUnicodeFormat)

I also tried each of these conversions to byte.

'Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(s)
'Dim bytes As Byte() = System.Text.Encoding.BigEndianUnicode.GetBytes(s)
'Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(s)
    Me.TextControl1.Load(bytes, TXTextControl.BinaryStreamType.InternalUnicodeFormat)

All four attempts generate an error.

The data stored in SQL hasn't changed. The vendor control has changed to .net and my program has moved to Visual Basic 2008. I think that I am just converting the SQL data incorrectly.
Of course, the vendor has left for the day and I am at different client tomorrow.

 
Private Sub frmInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim JIRConn As New System.Data.SqlClient.SqlConnection
        Dim JIRCmd As New System.Data.SqlClient.SqlCommand
        Dim JIRReader As System.Data.SqlClient.SqlDataReader = Nothing
        Dim ParmString As String = String.Empty
        Dim s As String = String.Empty
        Dim ba() As Byte = Nothing
        Dim enc As New System.Text.UTF8Encoding
        '
        JIRConn.ConnectionString = PublicVariables.JIRConnStringP
        JIRConn.Open()
        Application.DoEvents()
        '
        JIRCmd.Connection = JIRConn
        ParmString = "SELECT  Notes FROM JIRARNotes " & _
        "WHERE CusNo = 'P01800' and DocType = 1"
        JIRCmd.CommandText = ParmString
        JIRReader = JIRCmd.ExecuteReader
        Application.DoEvents()
        Do While JIRReader.Read
            If JIRReader.IsDBNull(0) Then s = "" Else s = JIRReader.Item("Notes").ToString.Trim
            Exit Do
        Loop
        JIRReader.Close()
        Application.DoEvents()
        '
        'Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(s)
        'Dim bytes As Byte() = System.Text.Encoding.BigEndianUnicode.GetBytes(s)
        Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(s)
        ba = enc.GetBytes(s)
        Me.TextControl1.Load(bytes, TXTextControl.BinaryStreamType.InternalUnicodeFormat)

        JIRCmd.Dispose()
        Application.DoEvents()
        JIRConn.Close()
        JIRConn.Dispose()
        Application.DoEvents()
    End Sub

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
What's in the string?
0
 
wdosanjosCommented:
Give this a try:

Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(s)
Me.TextControl1.Load(bytes, TXTextControl.BinaryStreamType.InternalFormat)

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
Okay, near as I can figure the behavior in VB6 is just wrong/out of whack.

Let's assume .TextControl1 contains 3 bytes, whose values are 65, 66 and 67.  You're saying in the VB6 program currently you have:
ba = .TextControl1.SaveToMemory() 'ba is a byte array, now with three elements, ba(0) = 65, ba(1) = 66 and ba(2) = 67
s = StrConv(ba, vbUnicode) 'VB converts the three bytes into a string, but not quite a unicode string I don't think
WriteToSql(s) 'put the string "s" into SQL DB

Open in new window


In my tests the string "s" appears to contain four characters, "A", "B", "C" and a NULL character (a character with an ASCII value of 0) - and SQL appears to preserve that trailing NULL character when inserted into the database.

That means later, when you read that string from SQL you need to convert everything except the last character into bytes:
Dim s As String = ReadFromSql() 'Get the string from SQL db
Dim ba(s.Length - 2) As Byte 'Initialize an array with one less elements than there are chars in s
System.Text.Encoding.ASCII.GetBytes(s, 0, s.Length - 1, ba, 0) 'Convert s to byte(), start at char 0, up to the next-to-last char, put results in ba, start at element 0 of ba

Open in new window


Ideally, if your data is binary treat it as such - you may lose something in the process of converting it to a string and back.
0
 
mpdillonAuthor Commented:
tgerbert,
You are on the right track.
VB6's ba=StrConv(s, vbFromUnicode) does not yield the same result as Visual Basic 2008's System.Text.Encoding.ASCII.GetBytes(s, 0, s.Length - 1, ba, 0).

I have attached two files. They are wildly different. It is a lot more than a null at the end. In fact I didn't find the size of the file to change at all.

In both vb6 and vb 2008 I wrote the Text files with the following Do Loop.
For i = 0 to Length of the array
   s = s & " " & Cstr(ba(i))
next i

Can you help me find the method in VB 2008 that will convert the Text Field data from SQL in the same manner as VB6's StrConv?
thanks,
pat
0
 
mpdillonAuthor Commented:
I forgot to attach the files.
TextCtl-VS2008-ASCII.txt
TextCtlVB6.txt
0
 
mpdillonAuthor Commented:
The file, TextCtlVB6.txt contains some NULL characters.
0
 
Todd GerbertIT ConsultantCommented:
What kind of data is coming out of the control, binary, unicode, ascii?
0
 
mpdillonAuthor Commented:
I am not 100% sure. That may seem odd but I never needed to know. I never had to work with that raw data. All I had to know was how to convert it to be stored in SQL Server. The code s = StrConv(ba(),vbUnicode) does that. Then I needed to know how to get the data from SQL server back into the control and ba = StrConv(s,vbFromUnicode) does that.
I believe the tech guy told me the control always used Byte data. He is probably gone for the day but I will email him anyway (they are in Germany). I asked what format the data in each element of the Byte array is in. Was that the correct question?
0
 
Todd GerbertIT ConsultantCommented:
Well, you've got a group of bytes coming out of the control - those bytes could represent text, sound, pictures, etc. I assume it's some kind of text since the thing's named TextControl1 - but is it just straight plain text?
0
 
mpdillonAuthor Commented:
I was investigating how to use a VB6 control and pass this project a Byte array. I couldn't even do that. Well, that thread got hijacked and we discussed the actual conversion just as we did here.

Someone presented a solution that worked.
dim myBytes() as Byte = System.Text.Encoding.GetEncoding(1252).GetBytes("Hello World")

Thank you for staying with me.
pat
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now