T61 String Decoding

J

Jeffrey Walton

Hi All,

Here's the solution I've been able to put together thanks to Jon
Skeet. Poseted in case anyone else wanders upon this (or the previous
thread) while grepping the web for the solution.

Jeff
Jeffrey Walton

// Many thanks to Jon Skeet for the hand holding
private string GetT61String()
{
StringBuilder builder = new StringBuilder();

// CodePage Number: 20261, Name: x-cp20261, DisplayName: T.61
// See http://msdn2.microsoft.com/en-us/library/system.text.encodinginfo.aspx
StreamReader reader = new StreamReader(
new MemoryStream( Value, false ), // Byte[], Boolean writeable
Encoding.GetEncoding(20261)
);

int nBytesRead = 0;
char[] buffer = new char[BUFFER_SIZE];
while (0 != ( nBytesRead = reader.Read(buffer, 0, BUFFER_SIZE) ))
{
builder.Append(buffer, 0, nBytesRead);
}
reader.Close();
return builder.ToString();
}
 
J

Jon Skeet [C# MVP]

Jeffrey Walton said:
Here's the solution I've been able to put together thanks to Jon
Skeet. Poseted in case anyone else wanders upon this (or the previous
thread) while grepping the web for the solution.

1) If you've got a byte[] already, why are you creating a MemoryStream?
Just use Encoding.GetString.

2) Rather than looping round with StreamReader, it's much easier to use
ReadToEnd()

3) In general, streams and StreamReaders should be dealt with in
"using" blocks to make sure that they are disposed even in the face of
an exception.

4) Your nBytesRead variable is misnamed - it's not reading bytes, it's
reading *characters*.


Point 1) is the most important one, because your method body can be
transformed into:

return Encoding.GetEncoding(20261).GetString(Value);
 
J

Jeffrey Walton

Hi Jon,
1) If you've got a byte[] already, why are you creating a MemoryStream?
Just use Encoding.GetString.
2) Rather than looping round with StreamReader, it's much easier to use
ReadToEnd() ....
return Encoding.GetEncoding(20261).GetString(Value);
That was the piece of magic I was looking for. Thanks for the
corrections. I did not like going back into a Stream, but I'm too
ignorant with respect to the C# language.
3) In general, streams and StreamReaders should be dealt with in
"using" blocks to make sure that they are disposed even in the face of
an exception.
I was aware of this :)
4) Your nBytesRead variable is misnamed - it's not reading bytes, it's
reading *characters*.
I missed this detail on MSDN. Thank you again.


Jeff

Jeffrey Walton said:
Here's the solution I've been able to put together thanks to Jon
Skeet. Poseted in case anyone else wanders upon this (or the previous
thread) while grepping the web for the solution.

1) If you've got a byte[] already, why are you creating a MemoryStream?
Just use Encoding.GetString.

2) Rather than looping round with StreamReader, it's much easier to use
ReadToEnd()

3) In general, streams and StreamReaders should be dealt with in
"using" blocks to make sure that they are disposed even in the face of
an exception.

4) Your nBytesRead variable is misnamed - it's not reading bytes, it's
reading *characters*.

Point 1) is the most important one, because your method body can be
transformed into:

return Encoding.GetEncoding(20261).GetString(Value);
 

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