Solved

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

Posted on 2008-10-02
9
511 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
Comment Utility
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
Comment Utility
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
Comment Utility
You have to trim the leading null characters:

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

0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Sorry, I meant:
You have to trim the trailing null characters
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:Dnx_7
Comment Utility
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
Comment Utility
it is not char 10, it is char 0
0
 
LVL 2

Author Comment

by:Dnx_7
Comment Utility
:(

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
Comment Utility
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
Comment Utility
IT WOOOORKKKKSSS!!!

Thank youuuu very much!

Kind regards
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now