Problem with ASCIIEncoding.GetString 
Author Message
 Problem with ASCIIEncoding.GetString

Hello,

I'm having a problem converting a byte array to a string.  
In the VS.NET de{*filter*}, the string that's converted looks
like "MyString instead of "MyString".  The ending
quotation mark isn't there, and it's causing problems when
I'm using a Mid function to capture a few characters from
the end of the string (i.e. "MyString apparently isn't the
same as "MyString").  Below is my code.

Dim r_bytes(200) As Byte
r_s.Receive(r_bytes)

Dim r_string As String = r_string &
System.Text.Encoding.ASCII.GetString(r_bytes, 0,
r_bytes.Length)

If r_string.Length > 0 And Mid(r_string, 1, 1) = "R" Then

Dim tmpProgramID As String = Mid(r_string, 2,
tmpAtLocation - 2)
Dim tmpLicenseID As String = Mid(r_string, tmpAtLocation +
3)

Any help or suggestions are appreciated.  Thanks!

Mike



Mon, 31 Jan 2005 06:39:28 GMT  
 Problem with ASCIIEncoding.GetString

Quote:
>I'm having a problem converting a byte array to a string.  
>In the VS.NET de{*filter*}, the string that's converted looks
>like "MyString instead of "MyString".  The ending
>quotation mark isn't there, and it's causing problems when
>I'm using a Mid function to capture a few characters from
>the end of the string (i.e. "MyString apparently isn't the
>same as "MyString").  Below is my code.

[code snipped]

Quote:

>Any help or suggestions are appreciated.  Thanks!

I can reproduce the problem at my side.

However, I notice if you append a char to the byte, the " will be
tailed.

A workaround:

        Dim r_bytes(26) As Byte
        Dim i As Integer
        Dim s As String

        For i = 0 To 25
            r_bytes(i) = Asc("A") + i
        Next

        r_bytes(26) = Asc(" ") ' Tailing with a char

        s = System.Text.Encoding.ASCII.GetString(r_bytes, 0,
r_bytes.Length - 1) ' length - 1

        Debug.Write(s)

HTH.

--
Peter Wu
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no
rights.



Mon, 31 Jan 2005 12:44:30 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Using ADO GetString with MSHFlexgrid - problem with memo field

2. GetString Problem

3. ado recordset.getstring problem

4. ADO.NET GetRows or Getstring ?

5. GetString and Clip

6. GetString and Clip

7. What is the reverse of GetString?

8. getrows or getstring

9. ADO abd Getstring Method

10. GetString limitations

11. Out of space using GetString?

12. Help with getstring, please

 

 
Powered by phpBB® Forum Software