PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Encoding.GetString

 
 
Alcibiade
Guest
Posts: n/a
 
      14th Mar 2011
Hi guys,
I'm working with the above method but I'm getting some issue....
What does the source at
http://msdn.microsoft.com/en-us/libr...aff.aspx#Y1503 ???

I'd expect it printed :"this is a test" but instead I get
this is a test\0\0\0\0\0\0\0\0\000\ and so on.....

Is it right?
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      14th Mar 2011
On 14-03-2011 07:28, Alcibiade wrote:
> Hi guys,
> I'm working with the above method but I'm getting some issue....
> What does the source at
> http://msdn.microsoft.com/en-us/libr...aff.aspx#Y1503 ???
>
> I'd expect it printed :"this is a test" but instead I get
> this is a test\0\0\0\0\0\0\0\0\000\ and so on.....
>
> Is it right?


Try:

Encoding.UTF8.GetString(bytes, 0, i)

Arne
 
Reply With Quote
 
 
 
 
Alcibiade
Guest
Posts: n/a
 
      14th Mar 2011
it's ok, but I'm wondering what the code should do...



Il 14/03/2011 14:42, Arne Vajhøj ha scritto:
> On 14-03-2011 07:28, Alcibiade wrote:
>> Hi guys,
>> I'm working with the above method but I'm getting some issue....
>> What does the source at
>> http://msdn.microsoft.com/en-us/libr...aff.aspx#Y1503 ???
>>
>> I'd expect it printed :"this is a test" but instead I get
>> this is a test\0\0\0\0\0\0\0\0\000\ and so on.....
>>
>> Is it right?

>
> Try:
>
> Encoding.UTF8.GetString(bytes, 0, i)
>
> Arne


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      14th Mar 2011
On 14-03-2011 10:19, Alcibiade wrote:
> Il 14/03/2011 14:42, Arne Vajhøj ha scritto:
>> On 14-03-2011 07:28, Alcibiade wrote:
>>> Hi guys,
>>> I'm working with the above method but I'm getting some issue....
>>> What does the source at
>>> http://msdn.microsoft.com/en-us/libr...aff.aspx#Y1503 ???
>>>
>>> I'd expect it printed :"this is a test" but instead I get
>>> this is a test\0\0\0\0\0\0\0\0\000\ and so on.....
>>>
>>> Is it right?

>>
>> Try:
>>
>> Encoding.UTF8.GetString(bytes, 0, i)

> it's ok, but I'm wondering what the code should do...


If the code reads 14 bytes, then the should only convert
those 14 bytes to a string not the entire 256 bytes
in the buffer.

Arne
 
Reply With Quote
 
Alcibiade
Guest
Posts: n/a
 
      14th Mar 2011
Il 14/03/2011 15:29, Arne Vajhøj ha scritto:

> If the code reads 14 bytes, then the should only convert
> those 14 bytes to a string not the entire 256 bytes
> in the buffer.
>
> Arne


yeah, I think so, instead I get a full lenght string :S
Is this behaviour normal?
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      14th Mar 2011
On 14-03-2011 13:24, Alcibiade wrote:
> Il 14/03/2011 15:29, Arne Vajhøj ha scritto:
>> If the code reads 14 bytes, then the should only convert
>> those 14 bytes to a string not the entire 256 bytes
>> in the buffer.
>> Arne

>
> yeah, I think so, instead I get a full lenght string :S
> Is this behaviour normal?


It is how GetString with 1 and 3 arguments are documented to
work.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      14th Mar 2011
On 14-03-2011 13:35, Peter Duniho wrote:
> On 3/14/11 10:24 AM, Alcibiade wrote:
>> Il 14/03/2011 15:29, Arne Vajhøj ha scritto:
>>
>>> If the code reads 14 bytes, then the should only convert
>>> those 14 bytes to a string not the entire 256 bytes
>>> in the buffer.

>>
>> yeah, I think so, instead I get a full lenght string :S
>> Is this behaviour normal?

>
> If you tell Encoding.GetString() to decode the entire byte[] buffer,
> even though you have not received enough bytes to fill the entire
> buffer, then you will see the results you are seeing. It is normal to
> see those results in that case, but it is not _correct_ for your own code.
>
> In other words, _your_ code has a bug in it. The behavior you are seeing
> is normal, given the bug in your code. You need to fix your bug, by
> passing to the GetString() method the range of the byte[] buffer you
> actually want decoded (i.e. the start index and the length), rather than
> just the buffer alone (which will decode the entire buffer).
>
> That's what Arne's original reply to you is saying to do, and it is what
> you should do. Once you've done that, you still will get "normal"
> behavior from the GetString() method, but that particular "normal"
> behavior will be the behavior you want, rather than behavior you don't
> want.


Yes.

But there is a little twist - the code is copied from
the MS docs.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      17th Mar 2011
On 14-03-2011 18:21, Peter Duniho wrote:
> On 3/14/11 3:03 PM, Bjørn Brox wrote:
>> [...]
>>> You told the computer to decode the entire buffer, so that is what it
>>> did. If you had told it to decode only part of the buffer then it
>>> would have done that.
>>>

>> Well, - go back to the first post, - it is Microsoft, not Arne who made
>> this error in their example.
>>
>> http://msdn.microsoft.com/en-us/libr...aff.aspx#Y1103

>
> It's true, the networking code examples in MSDN are not very good.
> However, it's important to note that that example is for the
> Socket.Receive() method, not the Encoding.GetString() method. It does
> correctly demonstrate the use of the Socket.Receive() method, albeit in
> a context where the rest of the code is not correct.


How the output from the call should be used is relevant for
the call.

> It is a very important skill to have, when reading documentation, to
> understand the scope of the documentation and to follow-up with the
> actual documentation for any other library or language elements that the
> code example might include.


I would expect the other code in the example to be correct. It may
be necessary to consult the documentation to the see what it does
or to do something differently. But having to consult the documentation
to find out what the code is not correct is absurd.

> So, yes…the original documentation being referenced isn't quite right.


That is an understatement. I would say that it is not right.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:42 AM.