Solved

StrConv equivilent in Visual Basic 2008

Posted on 2011-03-21
15
1,073 Views
Last Modified: 2012-05-11
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
Comment
Question by:mpdillon
  • 7
  • 6
  • 2
15 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35183775
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
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35183794
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35183842
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35183857
...in those examples the string literal "ABC" is actually Unicode (as wdosanjos pointed out, everything in .Net is unicode).
0
 

Author Comment

by:mpdillon
ID: 35184303
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
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35184334
What's in the string?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35184860
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35189746
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
 

Author Comment

by:mpdillon
ID: 35214746
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
 

Author Comment

by:mpdillon
ID: 35214793
I forgot to attach the files.
TextCtl-VS2008-ASCII.txt
TextCtlVB6.txt
0
 

Author Comment

by:mpdillon
ID: 35214811
The file, TextCtlVB6.txt contains some NULL characters.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35216403
What kind of data is coming out of the control, binary, unicode, ascii?
0
 

Author Comment

by:mpdillon
ID: 35216804
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35217790
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
 

Author Closing Comment

by:mpdillon
ID: 35221841
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now