Link to home
Start Free TrialLog in
Avatar of Dnx_7
Dnx_7Flag for Belgium

asked on

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

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
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

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
Avatar of Dnx_7

ASKER

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
You have to trim the leading null characters:

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

Sorry, I meant:
You have to trim the trailing null characters
Avatar of Dnx_7

ASKER

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
it is not char 10, it is char 0
Avatar of Dnx_7

ASKER

:(

event the 0 make the same result...

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

regards...
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dnx_7

ASKER

IT WOOOORKKKKSSS!!!

Thank youuuu very much!

Kind regards