Solved

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

Posted on 2008-10-02
9
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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