Solved

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

Posted on 2008-10-02
9
515 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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22628749
Sorry, I meant:
You have to trim the trailing null characters
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

17 Experts available now in Live!

Get 1:1 Help Now