Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ascii to string conversion vb.net?

Posted on 2006-06-16
7
Medium Priority
?
654 Views
Last Modified: 2012-05-05
Hello experts I am having an issue with some converting from a byte to a string.  I am receiving a value from comport in bytes and I convert it to  a string.  The problem is the string is suppose to be 16 characters long  but when I read it in only 8 characters can be read at one time it breaks it up into two 8 character strings and I am not sure why.  I am trying to convert it to characters and see if that works.  If someone could explain in a little more detail what is going on with these lines of code that might help.

  Dim oEncoder As New System.Text.ASCIIEncoding
            Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
            '-------------------------------------------------------------
            If Not Me.InputStream Is Nothing Then Return oEnc.Getstring(Me.InputStream)


'here is the entire property
 Overridable ReadOnly Property InputStreamString() As string
              Get
            Dim oEncoder As New System.Text.ASCIIEncoding
            Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
            '-------------------------------------------------------------
            If Not Me.InputStream Is Nothing Then Return oEnc.Getstring(Me.InputStream)
        End Get
    End Property

I tried converting it to characters  
Overridable ReadOnly Property InputStreamString() As char()
              Get
            Dim oEncoder As New System.Text.ASCIIEncoding
            Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
            '-------------------------------------------------------------
            If Not Me.InputStream Is Nothing Then Return oEnc.GetChars(Me.InputStream)
        End Get
    End Property

The value gets read into my program using an event like so:

  Public Sub comhold_CommEvent(ByVal source As Rs232, ByVal Mask As Rs232.EventMasks) Handles comhold.CommEvent
     
        Dim sread As string
        sread = comhold.InputStreamString
End sub
0
Comment
Question by:tentavarious
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16922614
ASCIIEncoding class works with Unicode characters. Every Unicode character is two bytes.
0
 

Author Comment

by:tentavarious
ID: 16922638
ok, so how could I convert the bytes into chars then convert the chars into a string?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 1400 total points
ID: 16922679
Well, the simplest way that I see is to read bytes from the stream and add every byte to string casting it to Char type.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:tentavarious
ID: 16922722
Ok, so back to the top, how come the sread value in my code only gets 8 characters.  The comhold event has to fire twice to read in the entire string.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16922759
Because every character in the sread value contains two bytes from input stream. Instead of packing two butes to one character, you need to convert every byte to character.
0
 

Author Comment

by:tentavarious
ID: 16922824
ok so how would I go about doing that changing the input stream to this:
Overridable ReadOnly Property InputStreamString() As char()
              Get
            Dim oEncoder As New System.Text.ASCIIEncoding
            Dim oEnc As Encoding = oEncoder.GetEncoding(1252)
            '-------------------------------------------------------------
            If Not Me.InputStream Is Nothing Then Return oEnc.GetChars(Me.InputStream)
        End Get
    End Property

Then chaning sread to this:
  Dim sread() As char
 
I am not sure on how to convert the characters back into a string.
0
 

Author Comment

by:tentavarious
ID: 16922953
I was just missing around with conversions this seems to work so basically since it is being read in as bytes alls i would need to do is convert it to characters then use a ctype(char variable, string) to get it back to a string correct?

 Dim sval As String = "1234567890123456"
        Dim oEncoder As New System.Text.ASCIIEncoding
        Dim byteval As Byte() = oEncoder.GetBytes(sval)
        Dim newstring As Char() = oEncoder.GetChars(byteval)
        Dim stringfinal As String = CType(newstring, String)
    End Sub
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

773 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