tentavarious
asked on
Ascii to string conversion vb.net?
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.InputStr eam)
'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.InputStr eam)
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.InputStre am)
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
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.InputStr
'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.InputStr
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.InputStre
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
ASCIIEncoding class works with Unicode characters. Every Unicode character is two bytes.
ASKER
ok, so how could I convert the bytes into chars then convert the chars into a string?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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.InputStre am)
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.
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.InputStre
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.
ASKER
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
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