UTF8 Question

N

Nuno Magalhaes

Why does (in the code below) the message should appear as "keep-alive -
keep-alive - test" but it only appears as "keep-alive - keep-alive".
That is why the last comparation (Equals) isn't getting true. Is it
from the "Convert.ToByte('\0')" that does the comparation false?
I'm shure that the strings are identical but it gives me false. Why?

Any help is appreciated, (Possible some UTF8 compatibility issues?)
Nuno Magalhaes.

Here's the code:
***************************************************************
byte[] connectionBytes=new byte[16];
for(int i1=index+12,i2=0;;i1++,i2++)
{
if(bytes[i1]=='\n')
{
connectionBytes[i2]=Convert.ToByte('\0');
break;
}
connectionBytes[i2]=bytes[i1];
}
string strTemp=Encoding.UTF8.GetString(connectionBytes).ToLower();
MessageBox.Show("keep-alive - "+strTemp+" - test");
if(Encoding.UTF8.GetString(connectionBytes).ToLower().Equals("keep-alive"))keepAlive=true;
else keepAlive=false;
 
M

Mattias Sjögren

Why does (in the code below) the message should appear as "keep-alive -
keep-alive - test" but it only appears as "keep-alive - keep-alive".

Because the MessageBox will only show the string up to the first '\0'
character.

That is why the last comparation (Equals) isn't getting true. Is it
from the "Convert.ToByte('\0')" that does the comparation false?

Yes one string will have a null character at the end but the literal
"keep-alive" doesn't.


Mattias
 
N

Nuno Magalhaes

Thanks for the reply.
I solved the problem with a Substring(0,10) comparision.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top