Solved

Why String.length equals ZERO but the content equals ""

Posted on 2008-10-02
9
517 Views
Last Modified: 2013-12-17
Hi,

I have a byte array and i get a string (unicode format)

the result is strange sometimes

the string length equals ZERO and the content of this string is empty...

can you explain me?

Regards
0
Comment
Question by:Dnx_7
  • 4
  • 4
9 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22628463
What is your data in the byte array? Is there a termination character in it?

Can you put up what is in your byte array and your code for changing it to a string?

Cheers,

Greg
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22628572
surely a termination character

i receive it from a socket (tcp listener) and it is a string


                    ReDim bytes(128) '

                    mySocket.Receive(bytes, bytes.Length, SocketFlags.None)

                    eventReceived = System.Text.Encoding.Unicode.GetString(bytes)

i also have the problem to redim the byte array to exactly what i want to receive from the network...

Regards
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22628742
You have to trim the leading null characters:

Dim nulls As Char() = chr(0)
eventReceived = System.Text.Encoding.Unicode.GetString(bytes).TrimEnd(nulls)

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22628749
Sorry, I meant:
You have to trim the trailing null characters
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22628828
eventReceived = System.Text.Encoding.Unicode.GetString(bytes).TrimEnd(Convert.ToChar(10))

still have the same result... :(

the string is "bitlink 7" and the length equal 65
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22628902
it is not char 10, it is char 0
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22628927
:(

event the 0 make the same result...

eventReceived = System.Text.Encoding.Unicode.GetString(bytes).TrimEnd(Convert.ToChar(0))

regards...
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 22628987
try with this:

Dim recv as Integer
ReDim bytes(128)
recv mySocket.Receive(bytes, bytes.Length, SocketFlags.None)
eventReceived = System.Text.Encoding.Unicode.GetString(bytes, 0, recv)
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22629031
IT WOOOORKKKKSSS!!!

Thank youuuu very much!

Kind regards
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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